How to run bash scripts like a crontab for Mac? (Big Sur)
The trick is... there is no crontab on Mac since 6-7 years. You must use launchctl
to schedule a "cron job".
In my case, I simply want to run a bash script for my local backup. I guess that if you are confortable running cron jobs on Linux, you will be at home with these instructions.
How To
launchctl unload ~/Library/LaunchAgents/com.pascalandy.macbackup.plist
cd ~/Library/LaunchAgents
nano com.pascalandy.macbackup.plist
<INSERT XML CODE BELOW>
<SAVE AND QUIT NANO>
launchctl load ~/Library/LaunchAgents/com.pascalandy.macbackup.plist
launchctl list | grep 'pascalandy'
Code example
<?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>Label</key>
<string>com.pascalandy.macbackup</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>/Volumesxyz/pascalandy/macbkp/runup.sh</string>
</array>
<key>LowPriorityIO</key>
<true/>
<key>Nice</key>
<integer>1</integer>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>11</integer>
<key>Minute</key>
<integer>39</integer>
</dict>
</dict>
</plist>
(XML format)
Sources
If you need more blabla, there you go :)
- https://alvinalexander.com/mac-os-x/mac-osx-startup-crontab-launchd-jobs/
- https://thejandroman.wordpress.com/2013/02/13/introduction-to-launchd/
- https://www.launchd.info/
- https://firepress.org/en/how-to-run-bash-scripts-like-a-crontab-for-mac-big-sur/