Six years ago, I posted about using rsync to backup a web server (many of the difficulties mentioned there have since been fixed). I’m finally getting around to switch from using cron to run that backup to using the new hotness: launchd. This task was made easier by Preston Holmes’ cron to launchd converter.

To summarize the prior post, I have a special user on my Mac named “backup”. The only thing this user does is backup other stuff, such as my web server. This user has a script (shown in the previous post) in ~/Documents/webbackup. I want this script to run at, say, 2:30am as this backup user, even if no one is logged on. To do this with launchd, I first need a file describing the job:

<?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>
  <!-- Write this file to /Library/LaunchDaemons/ -->
  <key>Label</key>
  <string>com.mycompany.webbackup</string>
  <key>UserName</key>
  <string>backup</string>
  <key>ProgramArguments</key>
  <array>
    <string>/Users/backup/Documents/webbackup</string>
  </array>
  <key>StartCalendarInterval</key>
  <array>
    <dict>
      <key>Hour</key>
      <integer>2</integer>
      <key>Minute</key>
      <integer>30</integer>
    </dict>
  </array>
  
  <key>StandardErrorPath</key>
    <string>/Users/backup/Documents/webbackup.log</string>  
   <key>StandardOutPath</key>
  <string>/Users/backup/Documents/webbackup.log</string>
</dict>
</plist>

While there are ways to run this job using launchctl, if you want it to run automatically, unassisted, even after rebooting your machine, you need to write the file to /Library/LaunchDaemon/com.mycompany.webbackup.plist. Note, that is /Library, not ~/Library, so you will likely need to use sudo to get the file there.

Also note that the name of the file should match the “Label” key in the file, with .plist suffix. Note also that this “Label” is used as a unique id, so if you make more than one of these job descriptions you need to change both the filename and the “Label” key.


Published

Category

Technology

Tags