Using Launchd on Mac OS X

From Exterior Memory
(Redirected from Cron jobs on Mac OS X)
Jump to: navigation, search

Mac OS X has never used cron for timed execution of scripts. Starting with Mac OS 10.4, all processes are started with launchd, which not only runs startup scripts, but also runs cron jobs. Before 10.4 StartupItems were used.

The following configuration runs the script /usr/local/ both at startup, as well as every 2 minutes (120 seconds).

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">

Depending on the usage, you should place this file either in /Library/LaunchDaemons or /Library/LaunchAgents.

If your job needs to run even when no users are logged in.
If it is only useful when users are logged in.

Before you install the file, decide on it's identifier. The above script is identified by com.example.myscript, as shown in the Label field. Save this file as /Library/LaunchAgents/com.example.myscript.plist (using the key you have chosen).

This file will be automatically loaded and acted upon after reboot. In addition, you can manipulate it using the launchctl tool. To load and run this script without rebooting, do the following:

sudo launchctl load /Library/LaunchAgents/com.example.myscript.plist
sudo launchctl start com.example.myscript

You can verify it's status using

launchctl list