Launching a backup with launchd

March 17th, 2013 — Wordman

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" "">
<plist version="1.0">
  <!-- Write this file to /Library/LaunchDaemons/ -->


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.