This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new!


CLI Magic: Make time for crontab

By Mark Alexander Bain on August 22, 2005 (8:00:00 AM)

Share    Print    Comments   

Last week we looked at the at command, which will run a set of commands once and once only. For more complex regular scheduling, get to know your crontab file.

Why use a file to manage periodic jobs when a simple command can do about the same job? Well, suppose you create a script to submit at commands periodically. For example, if you have a file myscript that runs a series of commands and includes as a final command at now + 1 week &lt myscript, then the script will automatically resubmit itself once a week. Let's say the script carries out a backup every Sunday at 16:00. What if one weekend the server goes down, and doesn't come back up until Monday at 10:00? As soon as the server comes back up it will fire off all at jobs that have passed their activation times. When our script re-submits itself it will do it at 10:00 each Monday -- not what we want. That is why we will now look at regular scheduling using crontab.

Like most Linux commands, there are any number of explanations for the name "crontab" -- the one I prefer is c(h)ronological table. To begin using it, type crontab -e to invoke a text editor on the file. By default crontab -e uses vi. To change the default editor to, say, pico, add to your .profile the line:
export VISUAL=pico

The key to crontab is to remember the table structure:

  • Minute
  • Hour
  • Day of the month
  • Month
  • Day of the week
  • Command

So, for instance, the line
0 2 * * Sunday tar -cvf /mnt/dvd/backup$(date +"%Y%m%d").tar ~/*
will create a backup of your home directory every Sunday at 02:00. Literally it means "carry out the command on the hour at 2 a.m. every Sunday." Similarly,
0 0 1 january * echo "Happy New Year" | mail -s Greeting
sends an email every January 1 at midnight.

We can do a bit more with times. The following example emails a schedule file at 7:30 a.m. every weekday. Notice the use of a dash to denote a range:
30 7 * * mon-fri echo "Your schedule" | mail -s "Your Schedule: "$(date +"%d-%b-%Y") -a schedule.txt

As well as ranges you can use time divisions. To get crontab to run a task every 10 minutes you could type 0,10,20,30,40,50 in the minutes field, or you can use the much neater form of */10. You can mix the forms, so to run a process hourly during the day and every two hours during the night you would type:
0 9-17,18-8/2 * * * myprocess

As with at, you can limit who can use crontab, but in a slightly differently way. If /etc/cron.allow exists then only the users listed there can use crontab. If it doesn't exist but /etc/cron.deny does, then everyone except the users listed there may use crontab. If neither file exists then everyone may set schedules.

Viewing crontab jobs

You can view jobs in two ways: either use the crontab -e command, or, if you don't want to go into the editor, use crontab -l. Both will show the details that you have typed into the crontab file, making it easy to see what is due to happen and when. As with at you can see only your own jobs unless you log on as root.

Here's a tip: Don't remove a listing from crontab unless you are really, really sure that you no longer need it. Instead, put a hash (#) at the start of the line to disable that particular entry.

Let's go back to the example we started with. If a server goes down and is then brought back up, crontab scheduling just continues from that point. This means that if you had a job scheduled at 8:00 a.m. on a Monday but the server doesn't come online until 09:00, that slot will be missed and your job won't run this time around. However, it will run the following Monday at 08:00 (assuming that the server is up at that time, of course).

With at and crontab you can start automating regular tasks. No longer do you have to be up at 4:00 a.m. to do that backup; your database can be updated during the night; everyone's schedule can be emailed to them before anyone is even in the office. And all of this while you sit back and dream of how else you can use these simple and effective tools.

Share    Print    Comments   


on CLI Magic: Make time for crontab

Note: Comments are owned by the poster. We are not responsible for their content.

One other important point ...

Posted by: Anonymous Coward on August 23, 2005 01:42 AM
I think these short articles introducing command-line tools are a very good idea. However, this article omits one point which is, in my experience, a major source of errors for new 'crontab' users.

As the man pages note, the initial context in which a cron job starts is not necessarily the same as the context in the user's interactive shell. In particular, PATH, which is frequently set or modified in shell start-up files, is often different for a cron job. Other environment settings may be different, too.

If you are new to cron, I'd suggest that you'll probably save yourself some grief and "WTF?" moments by carefully reading your system's man pages (use 'apropos cron' to find them).

Rich Gibbs


Pain relief

Posted by: Anonymous Coward on May 30, 2006 01:06 AM
[URL=<nobr>.<wbr></nobr> htm] Nerve pain relief [/URL]
[URL=<nobr>k<wbr></nobr> pain.htm] Low back pain [/URL]
[URL=<nobr>b<wbr></nobr> ackpainrelief.htm] Back pain relief [/URL]
[URL=<nobr>l<wbr></nobr> es/kneepainrelief.htm] Knee pain relief [/URL]
[URL=<nobr>e<wbr></nobr> lief.html] Pain relief [/URL]
[URL=<nobr>i<wbr></nobr> nrelief/painreliefpreved.htm] Pain relief [/URL]
[URL=<nobr>t<wbr></nobr> ion-Pain-Relief.html] Medication pain relief [/URL]
[URL=<nobr>l<wbr></nobr> -Pain-Relief.html] Natural pain relief [/URL]

[URL=] Pain relief [/URL]
[URL=] Back Pain [/URL]
[URL=] Pain relief [/URL]
[URL=] Pain relief [/URL]


More crontab majick: days of month

Posted by: Anonymous Coward on August 23, 2005 04:54 AM
So some typical issues of crontab recipies involve running jobs on a specified schedule that doesn't particularly meet well with cron's specifications.

Since your crontab entries are effectively short (usually) shell scripts, you can invoke shell logic and an appropriate shell script to run a job on the nth weekday of the month (eg: 3rd Monday), the last day of the month, the last given weekday of the month, the last workday of the month (well, modulo holidays), etc.

See a discussion of such scripts on the Linux Elitists list:

<a href="" title=""><nobr>h<wbr></nobr> /011105.html</a><nobr> <wbr></nobr>... and note the bugfixes<nobr> <wbr></nobr>;-)

Assuming a job to run on the third Monday of the month, you could simply run, every Monday: nthday 3 && myjob.

The logic can be easily extended to similar day-of-the-week tricks.

Karsten M. Self (<a href="" title=""></a>)


That tar command...

Posted by: Anonymous Coward on August 23, 2005 07:17 PM
0 2 * * Sunday tar -cvf<nobr> <wbr></nobr>/mnt/dvd/backup$(date +"%Y%m%d").tar ~/*

I suggest you make that

0 2 * * Sunday tar c --one-file-system -f<nobr> <wbr></nobr>/mnt/dvd/backup$(date +"%Y%m%d").tar ~/*

Or at least

0 2 * * Sunday tar c --exclude="mnt/dvd/backup*.tar" -f<nobr> <wbr></nobr>/mnt/dvd/backup$(date +"%Y%m%d").tar ~/*


Re:That tar command...

Posted by: Anonymous Coward on August 29, 2005 11:15 AM
Unless ~ is on<nobr> <wbr></nobr>/mnt/dvd what's the point? and wtf would ~ be doing there?


shell script

Posted by: Anonymous Coward on February 09, 2006 01:13 AM
# backup and create tar and archive for selected extension (<nobr> <wbr></nobr>.pdf,<nobr> <wbr></nobr>.xls,<nobr> <wbr></nobr>.doc,<nobr> <wbr></nobr>.ppt ) with time tag (like mmhhddmmyy format) from entire hdd
if the backup time and date are 12:40 8th feb 2006 then the file name shoudl be "4012080206.tar.gz" format.

Create also the list of the files which were backed up with the same time tag and name with above example "list.4012080206.tar.gz"

set the schedule everyday morning 8:30 AM

Archive backup older than 15 days should be delete automatically.

  Is it possible? if yes mail script at


$(date +"%Y%m%d") in crontab is not going to work

Posted by: Administrator on October 23, 2006 02:43 AM
Have you actually tried this ?
Here is what you get in the error message:<nobr> <wbr></nobr>/bin/sh: -c: line 1: unexpected EOF while looking for matching `)'<nobr> <wbr></nobr>/bin/sh: -c: line 2: syntax error: unexpected end of file

You need to escape the % in crontab: $(date +\%Y\%m\%d)


Re: $(date +"%Y%m%d") in crontab is not going to work

Posted by: Anonymous [ip:] on December 27, 2007 09:27 AM
Thank you for a lot!!!


This story has been archived. Comments can no longer be posted.

Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya