I have written a Ruby script that I need to run every 12 hours. Is there an easy way to do this in OS X (specifically Lion)?
There are a couple of ways of doing this, but the details depend on a couple of questions: Does the script need to run as root (Admin) or a regular user? Does it need to run on any specific schedule, or just a 12-hour interval?
The OS X-ish option is to create a LaunchDaemon. Create a file named /Library/LaunchDaemons/local.scriptname.plist, with contents like this:
Notes: 43200 is 12 hours in seconds, meaning that this will every 12 hours starting at boot. You should replace scriptname with a more appropriate identifier in both the Label value and the filename (and if you're going to publish this, replace "local." prefix with your domain name in reverse order, e.g. "example.com.scriptname"). If it should run as someone other than root, add:
If you need it to run at particular times of day, replace the StartInterval key and value with something like this:
...replacing Hours 6 and 18 (6pm) with the hours you want it to run.
The other option is to use cron, which is more generic-unix (but works fine on OS X). Use the command
Note that there's no equivalent of StartInterval here, this always runs it at 6am and 6pm.
|show 2 more comments|
Great advice above. However, I'd have to say using cron is the best, most robust solution. Not only is it stable; it will also prepare you to work in other environments outside of OS X.