[Courses] Linux commands: /etc/crontab, crontab

Carla Schroder carla at bratgrrl.com
Mon Feb 9 11:17:38 EST 2004


Cron jobs are cool and easy, once you figure them out. (Like everything on 
Linux!)

There are two cron utilities: /etc/crontab, and the crontab command. I 
like /etc/crontab because it puts everything in one place. Only root can 
edit /etc/crontab. The 'cat' command is a nice safe way to view files:

~~~~~~~~~~~~

$ cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file.
# This file also has a username field, that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
25 6    * * *   root    test -e /usr/sbin/anacron || run-parts 
--report /etc/cron.daily
47 6    * * 7   root    test -e /usr/sbin/anacron || run-parts 
--report /etc/cron.weekly
52 6    1 * *   root    test -e /usr/sbin/anacron || run-parts 
--report /etc/cron.monthly
#
#shutdown the system every evening at midnight, with five minutes warning
00 0   * * *   root /sbin/shutdown -h +5

~~~~~~~~~~~~

Ignore the anacron stuff, or whatever else your system may have stuffed in 
there. This is the important bit:

# m h dom mon dow user  command

this is what all those things mean:

              field          allowed values
              -----          --------------
              minute         0-59
              hour             0-23
              day of month   1-31
              month          1-12 (or names, see below)
              day of week    0-7 (0 or 7 is Sun, or use names)

The asterisk * means "all of them."  So this command shuts down the system 
every night at midnight:

# m h dom mon dow user  command
00 0   * * *   root /sbin/shutdown -h +5

Suppose we just want it to shut down on weekends?

#shutdown at 1:05am Saturdays and Sundays
01 05   * * 7,0   root /sbin/shutdown -h +5

Sometimes Sunday is 7, you'll have to try it to see what works:

01 05   * * 6,7   root /sbin/shutdown -h +5

It would be nice to use 'sat,sun', but you can't list names. Day-of-the-week 
and months can use names, use the first three letters: sat, sun, jan, feb. 
Case does not matter.

You can use ranges: 1-4 means 1,2,3, and 4
Ranges and lists can be mixed: 1,3,5,6-10

Step values are kewl. Step values follow ranges:

10-23/2  means every second hour in the range
*/2 in the dow field means every other day

See man 5 crontab for yet more crontab fun! Remember the man -f command to 
find all man files on a subject:

$ man -f crontab
crontab (1)          - maintain crontab files for individual users (V3)
crontab (5)          - tables for driving cron

***crontab command***

This lets users set their own cron jobs. First look to see if there are any 
existing crontabs for you:

$ crontab -l

To create or edit one:

$ crontab -e

This will open in the default editor, which is defined either in your .bashrc 
or .bash_profile. Create a new cron job, just like in /etc/crontab, with one 
exception: there is no user field.

Only root can edit crontabs for other users:

# crontab -u carla -e

Use -r to delete a crontab. When you're editing a crontab, ignore the filepath 
displayed in your editor, it looks something like this:

/tmp/crontab.STYJqN/crontab

Do not try to name the file, simply save & close, and it will automagically 
end up in the right place, /var/spool/cron.

crontab (5), crontab (1), cron (8)
-- 
~~~~~~~~~~~~~~~~~~~~~~~~~
Carla Schroder
www.tuxcomputing.com
this message brought to you
by Libranet 2.8 and Kmail
~~~~~~~~~~~~~~~~~~~~~~~~~



More information about the Courses mailing list