Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,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
#
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.
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (6 votes cast)
 
[133,719 views]  

Using the 'cron' scheduler | 5 comments | Create New Account
Click here to return to the 'Using the 'cron' scheduler' hint
The following comments are owned by whomever posted them. This site is not responsible for what they say.
gui to crontab
Authored by: sven on Wed, Feb 7 2001 at 3:20AM PST
I should note that there is a gui frontend for editing your crontab available. It's called CronniX, see www.koch-schmidt.de/cronnix. Incidentally, the author is called Sven, too... ;-)

[ Reply to This | # ]
gui to crontab
Authored by: robg on Wed, Feb 7 2001 at 8:37AM PST
Thanks Sven - it's posted somewhere here as a tip, too. Good program that makes configuring cron much more straightforward!

-rob.

[ Reply to This | # ]
MAKE SURE YOU...
Authored by: Xeo on Sun, Oct 28 2001 at 11:44PM PST
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 | # ]
MAKE SURE YOU...
Authored by: fullstop102 on Fri, Feb 11 2005 at 11:38AM PST
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!

Cheers

[ Reply to This | # ]
Using the 'cron' scheduler
Authored by: RandomMarius on Thu, Apr 17 2008 at 1:12PM PDT
Some other problems you may have:

When I opened up a GUI application using crond:

sh /usr/bin/open ~/Application.app

It did not seem to work.

I changed it to:

/usr/bin/open ~/Application.app >& /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:

"com.apple.launchd[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 | # ]