#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 plistis Apple Property List
- com.mydomain.foo.plistName of launchd plist file should be a reverse fqdn, like (this may not be required, but convention)
- com.mydomain.foo.plistlives in- $HOME/Library/LaunchAgentsand is ran as that user.
- com.mydomain.foo.plistcan also live- /Library/LaunchDaemonsor- /Library/LaunchAgents, have requirements, ran as- root
- Load plistwithlaunchctl load com.mydomain.foo.plist
- Unload plistwithlauchctl unload com.mydomain.foo.plist
- Check /var/log/system.logif 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