#launchd
Usage
I have a bash script called foo.sh
that takes one command line argument, bar
. I want it to run every 60
seconds and load at startup.
- an XML
plist
is Apple Property List com.mydomain.foo.plist
Name of launchd plist file should be a reverse fqdn, like (this may not be required, but convention)com.mydomain.foo.plist
lives in$HOME/Library/LaunchAgents
and is ran as that user.com.mydomain.foo.plist
can also live/Library/LaunchDaemons
or/Library/LaunchAgents
, have requirements, ran asroot
- Load
plist
withlaunchctl load com.mydomain.foo.plist
- Unload
plist
withlauchctl unload com.mydomain.foo.plist
- Check
/var/log/system.log
if you are having issues withplist
"If the system is turned off or asleep, cron jobs do not execute; they will not run until the next designated time occurs. If you schedule a launchd job by setting the StartCalendarInterval key and the computer is asleep when the job should have run, your job will run when the computer wakes up. However, if the machine is off when the job should have run, the job does not execute until the next designated time occurs."
<?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.mydomain.foo</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/foo.sh</string>
<string>bar</string>
</array>
<key>Nice</key>
<integer>1</integer>
<key>StartInterval</key>
<integer>60</integer>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/path/to/foo.err</string>
<key>StandardOutPath</key>
<string>/path/to/foo.out</string>
</dict>
</plist>
for reference:
- http://www.macresearch.org/tutorial_backups_with_launchd
- http://alvinalexander.com/mac-os-x/mac-osx-startup-crontab-launchd-jobs
- http://nb.nathanamy.org/2012/07/schedule-jobs-using-launchd/
- http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/ScheduledJobs.html
Hi,
Following your example, I have the following lines inside my foo.sh:
REDCap Cron Job (runs every minute)
(5 asterisks)
But I'm getting the following error message in foo.err:
/Users/ssd/bin/crontab-test.sh: line 2: Applications: command not found
I guess the old crontab format needs to be reformatted for launchd to understand it.
Can you give me some insight on this?
Best,
Daniel
dhinostroza AT gmail.com