views:

982

answers:

7

How do I have a script run every, say 30 minutes? I assume there are different ways for different OSs. I'm using OS X.

+1  A: 

Use cron.

Bill the Lizard
cron is legacy on OSX; Apple might actually decide in a distant future to just remove it and at best provide a compatibility layer that translates created cron tasks to launchd tasks if any (in case such a layer is really necessary, which I doubt).
Mecki
Cron is obsolete.
Mike Heinz
A: 

You can use the cron scheduler built into OS X. Here's a primer.

Ben Hoffstein
+4  A: 

On MacOSX, you have at least the following options:

From personal experience, cron is the most reliable. When I tested, launchd had a number of bugs and quirks. iCal alarms only run when you are logged in (but that might be something you prefer).

Bruno De Fraine
+11  A: 

Don't use cron as people have suggested. cron is legacy on OS X. Use launchd instead. It's different if you need to run it as system task (for all users) or as user task, just when your user is logged.

System tasks go to

/Library/LaunchDaemons/

if they shall run, no matter if any user is logged in at all. If they shall only run if any user is logged in, they go to

/Library/LaunchAgents/

if they shall run only if you are logged in, they go to

~/Library/LaunchAgents/

where ~ is your HOME directory.

Every task there is a file in plist format. It should have reverse domain name notation. E.g. you can name your task

com.example.my-fancy-task.plist

This plist can have various options and settings. Writing one per hand is suboptimal, you may want to get the free tool Lignon to create your task.

Just as an example, it could look like this

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.example.my-fancy-task</string>
    <key>OnDemand</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
     <string>/bin/sh</string>
     <string>/usr/local/bin/my-script.sh</string>
    </array>
    <key>StartInterval</key>
    <integer>1800</integer>
</dict>
</plist>

This agent will run the shell script /usr/local/bin/my-script.sh every 1800 seconds (every 30 minutes). You can also have task run on certain dates/times (basically launchd can do everything cron can do) or you can even disable "OnDemand" causing launchd to keep the process permanently running (if it quits or crashes, launchd will immediately restart it). You can even limit how much resources a process may use (as said before, Ligon shows all these settings in a nice UI interface).

Also you can manually enable/disable tasks via command line:

launchctl <command> <parameter>

command can be load or unload, to load a plist or unload it again, in which case parameter is the path to the file. Or command can be start or stop, to just start or stop such a task, in which case parameter is the label (com.example.my-fancy-task).

See also Apple's doku.

Mecki
What if I as a user want something to run even when I'm not logged in? Is that possible, or do I have to ask the admin to put something in /Library/LaunchDaemons?
Mark Baker
@Mark: yes, your admin would have to do this, and the UserName and GroupName keys would have to be set appropriately in the .plist file.
Steve Folly
Also note that for Leopard and beyond, OnDemand is deprecated in favour of the KeepAlive key. man launchd.plist for more details.
Steve Folly
+1  A: 

For apple scripts, I set up a special iCal calendar and use alarms to run them periodically. For command line tools, I use launchd.

Mike Heinz
+1  A: 

As Mecki pointed out, launchd would be the way to go with this. There's a GUI interface for launchd called Lingon that you might want to check out, as opposed to editing the launchd files by hand:

Lingon is a graphical user interface for creating an editing launchd configuration files for Mac OS X Leopard 10.5.

[snip...]

Editing a configuration file is easier than ever in this version and it has two different modes. Basic Mode which has the most common settings readily available in a very simple interface and Expert Mode where you can add all settings either directly in the text or insert them through a menu.

Some screenshots are available to to give you an idea of what it looks like:

Jay
A: 

FYI: while i do so miss it's siplicity, cron is a thing of the past on OS X. It was watchdog on panther. Since Tiger it has been launchd. So if you are running Leopard cron is not an option.