75

Cron? Launchd? iCal??

If so, how?

2

10 Answers 10

73

For reference, all 3 options would work. The iCal option however has more limitations than the others.

(There are GUIs for editing cron and launchd as mentioned in other answers)

Cron is the most straight forward and well known and there are many tutorials available. The Coles Notes is to add the last line from below to your crontab (either by editing /etc/crontab or using crontab on the command line):

MM HH DD MM WKD -- Minutes, Hour, Day, Month, Weekday (eg. Sun, Mon)
MM HH * * * USERNAME /PATH/TO/SCRIPT
00 3 * * * chealion /myscript.sh "Runs at 03:00 every day"

In Mac OS X, cron has actually been replaced by launchd but launchd is backwards compatible with cron meaning you can still use cron but it's actually launchd doing all the work.

If you want to use launchd you'll want to check out other questions here on Super User as well: (eg. How do I run a launchd command as root?) as to where you want to save your configuration file (the plist file) as when it runs depends on what directory it is stored in and how it's loaded (eg. whether you used sudo or not) - similar to cron.

A sample run daily launchd plist (be sure the file and and the label are the same - minus the plist for the label) follows - this script is run everyday at 3 minutes past midnight:

<?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">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>local.IDENTIFIER_HERE.SOMETHING</string>
    <key>KeepAlive</key>
    <false/>
    <key>RunAtLoad</key>
    <false/>
    <key>UserName</key>
    <string>USERNAME HERE</string>
    <key>Program</key>
    <string>/PATH/TO/SCRIPT</string>
    <key>ProgramArguments</key>
    <array>
        <string>Argument_1</string>
        <string>Argument_2</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>00</integer>
        <key>Minute</key>
        <integer>03</integer>
    </dict>
</dict>
</plist>

It's also worth noting that launchd tasks if they were scheduled for when the computer is asleep or off, they will run when the computer becomes available again (turning it on or waking it up) - though only once no matter how many days it may have been. Edit: I just was at an Apple document that said that if the machine is off, then you will lose any launch events during that time, (your script will not launch on startup), (sleep does launch script on waking up)


Another option is to use "at" (check manual page with "man at"). The script can reschedule itself with e.g.:

echo "sh $0 $@" | at `date +%H:%M` tomorrow

(use "+ 10 minutes" instead of "tomorrow" to run it every 10 minutes; to stop scheduling, just do "at -l" to list the scheduled job ids and then "at -r id" to remove the job)

You may have to start the corresponding daemon (atrun) first with (see https://superuser.com/a/43680):

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist

Advantages: quick fix, no sudo/root access needed, easy to do complicated schedule patterns Disadvantage: not standard scheduling method for OS X (which is launchd)

5
  • 10
    Sir, for your example launchd configuration file, I could kiss you. But I shan’t. Apr 2 '10 at 22:48
  • 5
    launchd is the way to go under OS X.
    – zneak
    Apr 3 '10 at 1:17
  • Yeah, that was my impression. It does sound like it had some issues back in Tiger, but Apple seem to have sharpened it up since then. Apr 3 '10 at 12:05
  • On Os 10.7.4 there is no /etc/crontab file. ): So I did 'which crontab' and that gave me /usr/bin/crontab but thats a garbled file.
    – john-jones
    May 3 '13 at 8:38
  • 1
    @HermannIngjaldsson /usr/bin/crontab is a program; if you run it in a Terminal with crontab -e, it will drop you into a terminal-based text editor on a file that will become your personal crontab when you save it.
    – Mark Reed
    Jan 11 '16 at 21:19
11

LaunchControl is another GUI for launchd/launchctl. While the alternatives mentioned by others are perfectly capable of launching a script on a daily basis, with LaunchControl you can configure complex schedules (like "once every weekday, on weekends every hour between 2PM and 8PM"). Also it is (AFAIK) the only tool which actually validates the job. If a job does not work as expected it will show you why.

It's free to try for as long as you want.

3
  • 3
    While it is free to try/use, the developer would prefer you pay for the license. sites.fastspring.com/somazone/product/shop
    – Kent
    Jan 27 '14 at 5:58
  • 1
    Absolutely. The reason why I posted this answer in the first place was to support the developer.
    – Mark Hall
    Jan 29 '14 at 3:16
  • Update: It's now paid software if you want to create any new launchD/launchCtl jobs. Currently ~USD$15 for personal use.
    – stwr667
    Feb 3 at 4:00
6

CronniX is a nice GUI frontend for scheduling cron jobs on the mac... Pretty decent if you don't want to go and delve into the cron documentation.

4

If you want to try out the FREE one, I would suggest Task Till Dawn

enter image description here

http://www.macupdate.com/app/mac/33283/task-till-dawn

4

Here's a simple, free utility to generate plist XML:

http://www.gieson.com/Library/projects/utilities/launcha/

With this util there's no need to install a program, just save the results to your Library/LaunchAgents folder.

1
  • Warning: if you use this tool, make sure that RunAtStart is then set to false (unless you want it true). I spent a hour figuring out why my Finder would freeze and stop the desktop from loading at boot. Disabling RunAtStart does not interfere with the fixed schedule. Mar 30 '19 at 21:07
2
  • For iCal you might want to read this article.
  • You should find enough tutorials for cron if you google for it.

It depends on what you want: iCal scripts will only be executed if you are logged in, cron executes the script without being logged in. I don't know if iCal executes a script if the Mac was turned of at the time the script should have been executed. cron is doing this.

2

An update: You can check out some apps in the Mac OS X App Store that do this:

Lingon also appears to be available as well, depending on which OS you are running.

1

I always use Lingon for this, but it looks like it's out of development. You could try Crontooie, but I've never used it personally. Or just edit the crontab file in the terminal.

2
  • It seems to me Lingon is under active development; the current version 3 is available in the AppStore along with the previous version 2. Both cost a few dollars and receive very mixed reviews. I personally use Lingon 3 with some success, but would not recommend it for the purpose in this article over other tools mentioned in other answers (cron, at, etc.).
    – ssc
    Jan 17 '13 at 11:36
  • Last time I used Lingon was over 2 years ago. At the time it worked fine for me, but I haven't used the recent versons. Too bad it's not as reliable as it once was.
    – Josh
    Jan 17 '13 at 12:47
0
sp@mactop ~ $ crontab -l
*/5 * * * * /usr/local/bin/cinco.sh
sp@mactop ~ $ cat /usr/local/bin/cinco.sh
#!/bin/sh
....cut....
sp@mactop ~ $ 

...whatever command-lines you throw in for "....cut...." will get run every five minutes.

30 years of progress, and 'cron' is still my favorite scheduler.

-C

0

If a calendar event meets your needs, the built-in Automator application also lets you create Calendar Alarms that are triggered by calendar events. You might use some of the built-in UI-based actions to do what you want, or you can also have it run some application or custom AppleScript, JavaScript, or shell script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.