launchd on Mac, part 2

In a previous post I wrote about my positive experience with a tool that automated the creation of launchd plist files for Mac, http://launched.zerowidth.com/. After several weeks of running, I found myself in a position of not knowing if the job was running as scheduled. A bit more searching yielded two interesting things.

First, I found that the plist syntax supports two keywords that allow me to capture the output of my script in files:

 <key>StandardErrorPath</key>
 <string>/Users/myname/backup.stderr.log</string>
 <key>StandardOutPath</key>
 <string>/Users/myname/backup.stdout.log</string>

I then changed the program arguments from this:

 <key>ProgramArguments</key>
 <array>
     <string>sh</string>
     <string>-c</string>
     <string>rsync -av -e ssh ~/Documents user@10.10.10.10:/home/user/backup/</string>
 </array>

to this:

 <key>ProgramArguments</key>
 <array>
     <string>sh</string>
     <string>-c</string>
     <string>date;rsync -av -e ssh ~/Documents user@10.10.10.10:/home/user/backup/</string>
 </array>

Now the stdout log file contains a timestamp for every time the task is run. I can simply open the file and scroll to the end when I feel uncertain about whether the task ran. 🙂

Second, I stumbled upon an excellent primer on launchd and the launchctl command that exercises it: http://nathangrigg.net/2012/07/schedule-jobs-using-launchd/

 

Advertisements