[Bash] Schedule Timed Jobs on macOS with `launchd`
Schedule timed jobs on macOS with launchd
launchd is a robust scheduled job automation tool on macOS that allows you to schedule a task to be run at recurring times. It's macOS's take on Linux's cron. In this lesson, we’ll schedule a simple bash script that empties the Trash to run every Monday morning at 10am.
First, we’ll create a plist file (a plist file is just XML) with our job configuration. In here, we’ll specify our bash script as the program to run. Then we move plist file to the ~/Library/LaunchAgents folder. Then we’ll use launchctl to load our configuration file, which will make the job active. We'll manually activate our job to ensure it's working with launchctl kickstart.
To install the launchd job:
mv local.empty-trash.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/local.empty-trash.plist
To verify it's active
launchctl list | grep 'local'
To run it immediately
launchctl kickstart gui/$UID/local.empty-trash
To unload the job
launchctl unload ~/Library/LaunchAgents/local.empty-trash.plist
Now if you grep for the job (launchctl list | grep 'local'
), it won't be in that list.
/local.empty-trash.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.cameronnokes.personal.empty-trash</string>
<key>ProgramArguments</key>
<array>
<string>/Users/cameronnokes/cron_empty-trash.sh</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Weekday</key>
<integer>1</integer>
<key>Hour</key>
<integer>10</integer>
</dict>
</dict>
</plist>
empty-trash.sh
#!/bin/bash
osascript -e 'tell app "Finder" to empty'
Additional notes
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.
Why not cron
?
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.
Why use AppleScript to empty the Trash and not rm -rf ~/.Trash
?
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.
Will my scheduled job run if my computer is off or asleep?
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."
Learn more
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2020-02-12 [Angular 8 Unit Testing] Angular 8 Unit Testing -- service
2020-02-12 [Algorithm] 448. Find All Numbers Disappeared in an Array
2019-02-12 [React + Functional Programming ADT] Create Redux Middleware to Dispatch Actions with the Async ADT
2019-02-12 [Algorithm] Fibonacci problem by using Dynamic programming
2019-02-12 [HTML5] Track First Contentful Paint with PerformanceObserver and Google Analytics
2018-02-12 [TypeScript] Generic Functions, class, Type Inference and Generics
2017-02-12 [NativeScript] Create new application and run emulator