Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Using the 'cron' scheduler System
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
#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/
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.
  • Currently 3.05 / 5
  You rated: 2 / 5 (19 votes cast)

Using the 'cron' scheduler | 14 comments | Create New Account
Click here to return to the 'Using the 'cron' scheduler' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
gui to crontab
Authored by: sven on Feb 07, '01 03:20:51AM

I should note that there is a gui frontend for editing your crontab available. It's called CronniX, see Incidentally, the author is called Sven, too... ;-)

[ Reply to This | # ]
gui to crontab
Authored by: robg on Feb 07, '01 08:37:44AM

Thanks Sven - it's posted somewhere here as a tip, too. Good program that makes configuring cron much more straightforward!


[ Reply to This | # ]
gui to crontab
Authored by: fyngyrz on Feb 12, '10 01:28:28PM

That URL is now spam.


[ Reply to This | # ]
gui to crontab
Authored by: tedw on Feb 12, '10 11:15:41PM
Authored by: Xeo on Oct 28, '01 11:44:20PM

If you cp /etc/crontab and use that as your base crontab, make sure you don't keep the user specification. I changed "root" to my user and spent hours trying to figure out why my crontab wouldn't work. It was trying to run my username as a command!

On user specific crontabs, do not specify a user to run the command as. The crontab located at /etc/crontab is the system-wide crontab. Root is specified in each so those commands are run as root. The user, root, doesn't have a crontab at all!!

This gave me a headache for a while, but I'm glad I understand it now.

[ Reply to This | # ]
Authored by: fullstop102 on Feb 11, '05 11:38:02AM

Thankyou so much for that.. Just my first time at setting up a cron for an rsync and taken me two hours of searching to find out why nothing was happening... You are a genious!


[ Reply to This | # ]
Using the 'cron' scheduler
Authored by: RandomMarius on Apr 17, '08 01:12:14PM

Some other problems you may have:

When I opened up a GUI application using crond:

sh /usr/bin/open ~/

It did not seem to work.

I changed it to:

/usr/bin/open ~/ >& /dev/null

And it worked.

I suspect not having the redirect-all at the end causes security problems as I not sure what uid's console is attached to the cron process at that time, and may be the cause of an error I received:

"[1] (0x10c650.cron[14768]): Could not setup Mach task special port 9: (os/kern) no access"

Hope this helps some people out.

PS: I used it to create template mails from an automator script I ran each morning to find people I know what has a birthday that same day.

[ Reply to This | # ]
Using the 'cron' scheduler
Authored by: robleach on Mar 25, '10 07:28:54AM

Oh wow, I hope that works! I've been so frustrated with cron and iCal script alarms not working in the middle of the night!


[ Reply to This | # ]
Using the 'cron' scheduler
Authored by: robleach on Apr 01, '10 08:02:20AM
Well, I can't confirm that it worked yet, but I have a tip to add. Turns out that the version of perl that root was using was different than the one my account was using. It would have been useful to have the STDERR output, so I changed the shell script call in my applescript wrapper to do something like this: 1> stdout 2> sterr;cat stderr stdout >
I set my cron file to run in a minute and it worked. Now I just have to see if it will work over night. Rob

[ Reply to This | # ]
Using the 'cron' scheduler
Authored by: icerunner on Jul 02, '10 02:00:08AM

Rather than copy the system cron file and edit in a text editor you can use the command

crontab -e

which brings up your crontab for editing in the 'vi' editor. When you save your changes it will then parse the crontab you have set up and warn you if there are any obvious errors in it.

man crontab
man 5 crontab
man vi

[ Reply to This | # ]
Using an external text editor for crontab
Authored by: Peter Kappesser on Feb 08, '11 07:23:19AM
TextWrangler's (and its "poppa" BBEdit's) edit command line tool can be used to edit the crontab file, following the setup instructions on its man page -- note that you have to write a little "helper" shell script to invoke edit with the -w option in order to work around a limitation in crontab.

I've used this for years, but it broke suddenly with the TextWrangler 3.5.3 update, with the error "crontab: temp file must be edited in place". This is because the new version of TextWrangler brought in some code from BBEdit which observes a new expert preference. To fix it, quit TextWrangler and execute the following in Terminal:

defaults write com.barebones.textwrangler Filing:SafeSavesDisabled -bool YES
I can't speak to the implications of disabling safe saves, however.

[ Reply to This | # ]
Using the 'cron' scheduler
Authored by: nwfrg on Jan 19, '11 05:11:15PM

crontab file locations under Mac OSX 10.6 are now:


[ Reply to This | # ]
Using the 'cron' scheduler
Authored by: davesnothere on May 18, '11 01:15:27PM

Found this old thread and while most of it seems to be helpful to me, there are some things I can't seem to make happen.

I'm completely new to *nix operating systems and just now getting into the guts of the OSX OS, so vim and the Terminal are fairly new to me.

That said, I have a need to run a cron process in root and I can't seem to make it happen.

I can't seem to get into the /usr/lib/cron/tabs directory, and I can't figure out what I need to do to read the cron.deny file in the /usr/lib/cron directory.

Thanks for any help.

Any vi tutorials or anything as well as any bash tutorials would be helpful as well.

[ Reply to This | # ]
Using the 'cron' scheduler
Authored by: coldkod on Sep 03, '11 08:22:28AM
To install the cron without using shell editor, check this site Install Cron Without Shell Editor

[ Reply to This | # ]