Use launchd instead of crontab on your Mac

As of Mac OS 10.4 crontab has been depreciated but not ended. So its a good idea to know how to use its replacement under Mac OS. Welcome to Launchd.

This will be a short guide on how to use launchd in a simple form to replace a crontab job. In my case I am automating the backup of my Plex server. You can see my previous article here

Launchd has a lot of other features. I will not look at them here.

You can checkout these other resources:

A basic example of a plist file for launchd. In this case we will use plexback.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>my.example.backupplex</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/caffeinate</string>
        <string>/User/ACCOUNT/bin/plexbackup.sh</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>6</integer>
        <key>Minute</key>
        <integer>2</integer>
        <key>Weekday</key>
        <integer>0</integer>
    </dict>
</dict>
</plist>

Note: Replace ACCOUNT with your login username.

You will need to create this file under your ~/Library/LaunchAgents/

If your system is always running and never shuts down. You will need to issue the command:
launchctl load -w ~/Library/LaunchAgents/plexback.plist
This will register your task with the scheduler that is used with launchd.


See also