launchd is a process on macOS used to manage agents and Daemons. It can be used to run shell scripts in a cron-like fashion, and is the more apple-esque way to do things. The launchtl command is used to interact with launchd.
- Create an executable file or script (remember to
chmod +x) and put it somewhere - Create a property list file -
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.yourusername.myjob</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/your/script.sh</string>
</array>
<!-- Example: Run every 300 seconds (5 minutes) -->
<key>StartInterval</key>
<integer>300</integer>
<!-- Optional: Keep the job alive (restart if it crashes/stops) -->
<!-- <key>KeepAlive</key>
<true/> -->
<!-- Optional: Redirect output for debugging -->
<key>StandardOutPath</key>
<string>/tmp/com.yourusername.myjob.stdout</string>
<key>StandardErrorPath</key>
<string>/tmp/com.yourusername.myjob.stderr</string>
</dict>
</plist>- Use the launchctl command to load the plist file into launchd, which will start the job running according to the schedule in the plist file
launchctl load ~/Library/LaunchAgents/com.yourusername.myjob.plist - The
launchctl unloadcommand and same path can be used if you want to stop this job. The path mentioned here in ~/Library/LaunchAgents is specific to third party agents (including your own)