To install the launchd job:
mv local.empty-trash.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/local.empty-trash.plistTo verify it's active
launchctl list | grep 'local'To run it immediately
launchctl kickstart gui/$UID/local.empty-trashTo unload the job
launchctl unload ~/Library/LaunchAgents/local.empty-trash.plistNow if you grep for the job (launchctl list | grep 'local'), it won't be in that list.
Launchd is actually a really huge topic and I only went over one of the most basic use cases of it. Here's some other things that I couldn't fit into the video.
Launchd is really robust and has a lot more features than cron does (at least that's the case on macOS, the GNU/Linux crontab is different and more robust than the BSD cron AFAIK). For example, some other possiblities:
- StartInterval: Run job every N seconds
- StartOnMount: Run when a device has been mounted (for example a backup harddrive)
- WatchPaths: Run when creating, removing files in this directory
- RunAtLoad: Run at startup and login
- StandardOutPath and StandardErrorPath: specify where your stdio goes (otherwise it goes to the syslog I think)
See "Learn more" below for more.
Apple also recommends using launchd over cron. For small use cases like this one, I think cron is fine though, I suspect that recommendation is targeted for macOS app devs.
There's a few edge cases with emptying the trash that rm doesn't handle. One of them was that if you have files from a different device in the trash then rm can run into permission issues deleting those.
From Apple's documentation: "If you schedule a launchd job by setting the StartCalendarInterval key and the computer is asleep when the job should have run, your job will run when the computer wakes up. However, if the machine is off when the job should have run, the job does not execute until the next designated time occurs."
http://www.launchd.info/ https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/ScheduledJobs.html https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html