Thursday, January 23, 2014

Cron Jobs on MacOSX using "launchctl"

(1) Create the following file

~/Library/LaunchAgents/local.mytask.plist

(2) The above file is an XML file - having contents like show in the example below

?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>KeepAlive</key>
        <false/>
        <key>Label</key>
        <string>local.mytask</string>
        <key>ProgramArguments</key>
        <array>
            <string>/Users/gbhujan/bin/take_code_backup.sh</string>
        </array>
        <key>StartInterval</key>
        <integer>120</integer>
        <key>RunAtLoad</key>
        <true />
        <key>StandardErrorPath</key>
        <string>/dev/null</string>
        <key>StandardOutPath</key>
        <string>/dev/null</string>
    </dict>
</plist>
In the above example, I run the script "/Users/gbhujan/bin/take_code_backup.sh" every 120 seconds

(3) Create the following file

~/.launchd.conf

(4) Having the following contents:

load /Users/gbhujan/Library/LaunchAgents/local.mytask.plist