Using Mac OS X `at` to schedule a daily task to clear out .Trash folders

1

2

How do I use the OS X at command to schedule a daily clean-up of the user's .Trash folder?

I'm thinking something along the lines of:

rm -rf ~/.Trash/**/* | at 0:00 + 1 day

Will this clean the trash every day?

We'll be running this command as part of a login script, so I also need a way to clear all the scheduled tasks. Is there a way to at -r all?

Chris Nolet

Posted 2013-02-25T03:12:54.117

Reputation: 113

1Is there any particular reason you want to use at vs cron/launchd? AT might not be better for this type of activity. – Eric G – 2013-02-25T03:34:43.240

Just so we can schedule it easily via a login script that runs every time the computer loads. Can we do this via cron? – Chris Nolet – 2013-02-25T03:37:44.113

The following question may be useful, if not, please let us know: http://superuser.com/questions/126907/how-can-i-get-a-script-to-run-every-day-on-mac-os-x

– Eric G – 2013-02-25T03:38:29.947

Yep, saw that. Not sure how to flush the jobs though. Do they flush on reset? Likewise, I've seen different syntax in different places - just wondering what the latest one is. – Chris Nolet – 2013-02-25T03:43:38.657

Answers

2

rm -rf ~/Trash/* often results in permission denied errors unless you run it as root. osascript -e 'tell app "Finder" to empty trash' would also show an error dialog if some files are in use.

You could try adding 0 0 * * * rm -rf ~/Trash/* to the root's crontab (sudo crontab -e).

Or using launchd, save a property list like this as /Library/LaunchAgents/empty_trash.plist (and make it owned by root):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>EnableGlobbing</key>
    <true/>
    <key>Label</key>
    <string>empty_trash</string>
    <key>ProgramArguments</key>
    <array>     
        <string>rm</string>
        <string>-rf</string>
        <string>/Users/username/Trash/*</string> <!-- ~/ doesn't work -->
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>0</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
</dict>
</plist>

It's loaded automatically on the next login, but you can load it immediately with sudo launchctl load /Library/LaunchAgents/empty_trash.plist.

Hazel also has options for emptying the trash automatically.

Lri

Posted 2013-02-25T03:12:54.117

Reputation: 34 501

Great - thanks! Just what I was looking for! – Chris Nolet – 2013-02-25T10:19:02.427