Say you’ve got a script somewhere that you want to run every 5 minutes on OSX. Something like this:
#!/bin/bash date >> /Users/Chris/Dashboard/dates.txt
Or perhaps your script is a bit more imaginative. Whatever. Make sure that it’s marked as executable:
chmod a+x mytask.sh
Now we need to make a plist to instruct launchd what to do with it. First, come up with a reverse-domain-style name for your task. I recommend something like com.mycompanyname.mydepartment.mytaskname. You’ll then name this file com.mycompanyname.mydepartment.mytaskname.plist. Here’s what the file needs to look like:
Note that you’ll want to change the task reverse-domain name and the path to your task in the above file. Also the interval, if you want it to run other than every 5 mins.
Next we need to install the task with launchd. First, copy it into your LaunchDaemons folder (or LaunchAgents, if you want it to only run when you’re logged in):
cp com.mycompanyname.mydepartment.mytaskname.plist /Library/LaunchDaemons
Finally, so that launchd will pick it up without needing a reboot, we do the following:
launchctl load -w /Library/LaunchDaemons/com.mycompanyname.mydepartment.mytaskname.plist
To check it’s all installed, do
launchctl list and check that your task is in the list.
When the time comes that you need to disable the task, do the following:
launchctl unload -w /Library/LaunchDaemons/com.mycompanyname.mydepartment.mytaskname.plist rm /Library/LaunchDaemons/com.mycompanyname.mydepartment.mytaskname.plist
Thanks for reading! And if you want to get in touch, I'd love to hear from you: chris.hulbert at gmail.
(Comp Sci, Hons - UTS)
iOS Developer in Sydney.
I have worked at places such as Google, News Corp, Fox Sports, NineMSN, FetchTV, Woolworths, and Westpac, among others. If you're looking for a good iOS developer, drop me a line!