Jump to content

OSX: Using the 'cron' scheduler


Leon

Recommended Posts

  • Administrators

UNIX includes a program named 'cron' to handle the execution of tasks on a specified schedule, regardless of whether the user is logged in or not. Cron does this through a series of simple text files known as 'crontabs' which control the scheduling of jobs.

 

The cron daemon is used by the system for scheduled daily, weekly, and monthly maintenance, and can be used by users to run various programs at set intervals, such as to handle my site backup program as described elsewhere on this site.

 

Read the rest of this article if you'd like a simple overview of what cron is and how it can be used.

 

The system cron tasks are stored in /etc/crontab. You can "cat" this file to get an example of what a crontab looks like:

user% cat /etc/crontab

# $NetBSD: crontab,v 1.13 1997/10/26 13:36:31 lukem Exp $

#

# /etc/crontab - root's crontab

#

SHELL=/bin/sh

PATH=/bin:/sbin:/usr/bin:/usr/sbin

HOME=/var/log

 

#min hour mday month wday user command

 

*/10 * * * * root /usr/libexec/atrun

 

# do daily/weekly/monthly maintenance

15 3 * * * root sh /etc/daily...

30 4 * * 6 root sh /etc/weekly...

30 5 1 * * root sh /etc/monthly...

 

[NOTE: There are TABS between the items, so don't copy/paste anything you see here; type it with tabs between fields!]

 

If you want even more info on what the system is actually doing for maintenance, follow the path under the COMMAND column, and "cat" the actual .sh files ... there's quite a bit going on for which you might like to leave your system running at least overnight on occasion!

 

The basic format of the file is relatively self-explanatory, sort of! An "*" in a column reads as "every", and the "*/10" in the first command line means "every 10th minute". So the weekly maintenance is set to run at 30 mins, 4 hours (or 4:30am) every day of every month, on the 6th day of the week.

 

After the scheduling block is a column for which user the command will run as (root in this case), and what the acutal command is that will be executed. You can add new system-wide tasks to the cron program by inserting lines into this file (but you must be 'root' in order to do so).

 

More applicable to a typical user would be to schedule their own cron task. I did just that for my backup solution mentioned in another hint. To do this, I first copied the root crontab to a file in my directory (cp /etc/crontab ~/mycrontab). I then edited the 'mycrontab' file to look like this (I've skipped the header, which I left the same as the root file):

#min hour mday month wday command

25 2,14 * * * sh path/to/getmybackup.sh

 

The 'sh getmybackup' runs my backup download shell script (see the related article on automated backups), and the time section is set to run it twice a day - at 2:25am, and 2:25pm (1425 in military time). Once the edited file has been saved, the final step is to tell the 'cron' program to schedule the task:

 

crontab mycrontab

That's all there is to it; the task is scheduled and will be executed twice a day at the specified time, as long as my machine is powered on. You can see a list of currently scheduled tasks by typing "crontab -l" at the command prompt, and you can cancel a crontab with "crontab -r".

 

Hope this helped shed a little light on the dark world of cron...and as usual, there's much more to this subject than I've covered here, but this should be enough to get you started.

 

 

Good way to edit your crontab on mac:

 

EDITOR=nano crontab -e

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...