Using launchd and launchctl to Schedule Jobs on MacOS
I've been using a Mac since 2013 for several reasons, but I never had the need to schedule anything that couldn't be done by a simple cronjob. However, I have slowly been moving towards tech independence. The first thing I did was to move away from Gmail for important e-mails after having read one too many a horror story on Hackernews about someone who suddenly lost access to their Google account.
The second step was to start moving away from Dropbox. I now have a mix of physical harddrives and a storage block. The first step was to start backing up writings, pictures and documents that don't live in Git repositories. Anyway, Kagi led me to this article: https://www.maketecheasier.com/use-launchd-run-scripts-on-schedule-macos/. Let's see how well this works.
I ended up creating this job:
<?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.user.rsyncbackup</string>
<key>ProgramArguments</key>
<array>
<string>/Users/haakon/scripts/rsync-backup.sh</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>20</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
<key>StartMissedJobs</key>
<true/>
</dict>
</plist>
I think it's the first time I wrote xml and not edited an existing piece of xml (which gives me flashback to parsing Powerpoint presentation XMLs in one of my first internships). After writing it I just added the job to launchctl with launchctl load ~/Library/LaunchAgents/com.user.mybackupscript.plist.