Schedule batch jobs - macOS

Get help for specific problems
Posts: 7
Joined: 25 Jul 2020

MikeF

I've found an alternative way of scheduling batch jobs in macOS, using the launchd service management framework.

It involves the following 2 steps (see example below):
1. Create a plist (property list) file
2. Load the batch job using launchctl

Example
1. Create a plist (property list) file
The following example runs FFS daily at 19:00. Note: the value of the 'Label' key can be anything, but the format shown is preferred (for subsequent identification - see 'launchctl list' below).
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!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>local.FreeFileSync.Personal</string>
        <key>StartCalendarInterval</key>
        <dict>
            <key>Hour</key>
            <integer>19</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
        <key>ProgramArguments</key>
        <array>
            <string>/Applications/FreeFileSync.app/Contents/MacOS/FreeFileSync</string>
            <string>/Users/mjf2708/Documents1/FreeFileSync/Personal.ffs_batch</string>
        </array>
    </dict>
</plist>
Save as '~/Library/LaunchAgents/local.FreeFileSync.Personal.plist'

2. Load the batch job using launchctl
Run the following from Terminal:
launchctl load ~/Library/LaunchAgents/local.FreeFileSync.Personal.plist
The batch job is now set up, and will run at the time specified.

To check the status of the job, type:
launchctl list | grep local.FreeFileSync.Personal
(the argument after grep is the 'Label' value, not the filename)

It will return something like
789    0    local.FreeFileSync.Personal
displaying the PID (or '-' if it is not running), and last return code.

(There is an extensive launchd tutorial here: https://www.launchd.info)