CRON JOBS; crontab, cron tables

November 24, 2010

Your crontab editor

The default editor for the ‘crontab -e’ command and many others is vi. On Ubuntu, it is the nano editor. To change it to something else, like emacs, or gedit, for instence, use the commmand
$ export VISUAL='gedit' (or whatever editor you like)
$ export VISUAL='emacs'

To see your crontab editor

What is your crontab editor?
to see what your crontab editor is, run the command “env” to see what “VISUAL” is set to:
$ env

if you don’t see VISUAL listed then it is still vi

To verify that cron is running

Cron is a daemon (crond), which means that it only needs to be started once, and will
lay dormant until it is required. A Web server is a daemon (httpd), it stays dormant
until it gets asked for a web page. The cron daemon, or crond, stays dormant
until a time specified in one of the config files, or crontabs, cron tables

If for some reason, you think cron is not running, you can check your running processes:
$ ps aux | grep crond

If it is running, you should get 2 lines something like this:
root 3570 0.0 0.1 6232 1128 ? Ss 11:14 0:00 crond
greg 5887 0.0 0.0 4548 656 pts/2 S+ 14:29 0:00 grep crond

root 3570 0.0 0.1 6232 1128 ? Ss 11:14 0:00 crond
shows that crond is running, the next line is the search
we just ran.

To see if you have a crontab file

attempt to list its contents “crontab -l” (letter L, not number 1)
$ crontab -l

A cron job is an entry in a crontab.
Structure of a cron job:

A cron job contains six fields.

 *     *     *     *     *     a_command_to_execute
 |     |     |     |     |
 |     |     |     |     +------- day of week (0 - 6) (0=Sunday) 
 |     |     |     +--------- month (1 - 12)
 |     |     +----------- day of month (1 - 31)
 |     +------------- hour (0 - 23)
 +--------------- min (0 - 59) 

minute hour day month day-of-week command-line-to-execute

crontab example:

# Execute script “/home/user/forwardmail” every minute of every day
* * * * * echo hello >> /home/[user]/Desktop/crontab-test.log
just to see it work, for a few minutes.

# Execute script “/home/user/trim.sh” every 5 minutes
*/5 * * * * /home/user/trim.sh
# Execute script once every minute, from 15 to 30 minutes past the hour, each hour.
15-30 * * * * /home/user/trim.sh

# Run the compute.sh script every 0 minutes, 10 minutes, and 50 minutes past the hours (0,10,50), between 9am and 5pm (9-17), every day of the year.
0,10,50 9-17 * * * ./compute.sh

# Execute script “/home/user/archive.pl” at 12:00 AM (midnight) every day
0 0 * * * /home/user/archive.pl

# Execute script “/home/user/bkup.pl” at 3:30 AM every Sunday
30 3 * * 0 /home/user/bkup.pl
# Execute script 5.00 PM every weekday (Mon-Fri)
0 17 * * 1-5 /home/user/bkup.pl

# Execute script on July 4th at 12:00 AM (midnight)
0 0 4 7 * /home/user/email-july4th-special.php

There are a few key words you can use instead:
# the @daily cron keyword will run your job at 00:00 on every day.
@daily /home/user/bkup.sh
# the @monthly cron keyword will run your job at 00:00 on 1st of every month.
@monthly /home/ramesh/suse/bin/tape-backup.sh
# the @yearly cron keyword will run your job at 00:00 on Jan 1st of every year.
@yearly /home/ramesh/suse/bin/annual-cleanup.sh


string meaning
------ -------
@reboot Run once, at startup. --- run a cron job every time the server is rebooted
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".

example:
@reboot sleep 22 ; echo hello >> /home/[user]/Desktop/crontab-test.log
on boot-up, wait for 22 seconds, then append ‘hello’ to the crontab-test.log file

Always use the absolute path. Cron job doesn’t run a script in the directory its found. Same for file references within the script.


crontab -e
To edit your cronjobs with your editor (see note at top).

Note
Never directly edit files in /var/spool/cron.
Cron will not be updated and you can potentially mess things up.
Instead use the command “crontab -e”

Yikes, Too Much Mail!

Did I mention that all output from commands, scripts, and error messages are mailed to you?
Guaranteed that when you first start using cron you’ll think you are being
mail-bombed. To combat this, add a ” > /dev/null” after the command/script, to redirect all
output to oblivion.

Listing and Removing Your Crontab File

To display the contents of your crontab file held in the /var/spool/cron directory, enter:

crontab -l
And to remove your crontab file, all your cronjobs, from the /var/spool/cron directory, enter:

crontab -r

FILES
/etc/cron.allow
/etc/cron.deny

Comments are closed.

We try to post all comments within 1 business day