Skip to content

launchd Cheat Sheet

List jobs

launchctl list

Stop a job

launchctl bootout gui/$(id -u) \ ~/Library/LaunchAgents/com.euwang.backup-config-files.plist

Load a job

launchctl bootstrap gui/$(id -u) \ ~/Library/LaunchAgents/com.euwang.backup-config-files.plist

Restart a job

launchctl kickstart -k \ gui/$(id -u)/com.euwang.backup-config-files

Create a LaunchAgent

Create the LaunchAgents directory if it does not exist:

mkdir -p ~/Library/LaunchAgents

Create a plist file:

vi ~/Library/LaunchAgents/com.euwang.backup-config-files.plist

Example plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.euwang.backup-config-files</string>

    <key>ProgramArguments</key>
    <array>
        <string>/Users/euwang/Projects/Java/Shell/backup-config-files.sh</string>
    </array>

    <key>RunAtLoad</key>
    <true/>

    <key>StartInterval</key>
    <integer>3600</integer>

    <key>StandardOutPath</key>
    <string>/tmp/backup-config-files.log</string>

    <key>StandardErrorPath</key>
    <string>/tmp/backup-config-files.err</string>
</dict>
</plist>

Validate the plist:

plutil -lint ~/Library/LaunchAgents/com.euwang.backup-config-files.plist

Expected output:

OK

Run a job every hour

Add StartInterval to the LaunchAgent plist:

<key>StartInterval</key>
<integer>3600</integer>

3600 means 3600 seconds, which equals 1 hour.

After editing the plist, reload the job:

launchctl bootout gui/$(id -u) \
 ~/Library/LaunchAgents/com.euwang.backup-config-files.plist

launchctl bootstrap gui/$(id -u) \
 ~/Library/LaunchAgents/com.euwang.backup-config-files.plist

View loaded jobs

launchctl print gui/$(id -u)

LaunchAgents location

~/Library/LaunchAgents

LaunchDaemons location

/Library/LaunchDaemons