Wednesday, September 17, 2008

Recurring jobs, launchctl, plist, and shell scripting on OSX

OSX is Unix - similar, and yet dissimilar to linux. I'm setting up a few recurring maintenance scripts

First of all, cron is deprecated in favor of launchd, which handles a lot more than just cron jobs.

First, recurring jobs are located in /Users/username/Library/LaunchAgent/ as .plist files.

Sample syntax of a .plist file:

<?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.thisismytask</string>
   <key>LowPriorityIO</key>
   <true/>
   <key>Nice</key>
   <integer>1</integer>
   <key>ProgramArguments</key>
   <array>
      <string>/Users/username/thisismytask.sh</string>
   </array>
   <key>StartInterval</key>
   <integer>3600</integer>
</dict>
</plist>


Alternative interval for daily
   <key>StartCalendarInterval</key>
   <dict>
      <key>Minute</key>
      <integer>0</integer>
      <key>Hour</key>
      <integer>5</integer>
   </dict>


Activate the job using the command launchctl load /Users/username/Library/LaunchAgents/plistname.plist

Deactivate the job the same way (except use unload)

No comments: