crontab allows you to execute commands at regular time intervals with the added bonus that you don't have to be logged in. crontab will have the output of your command mailed to you. You can specify the intervals in minutes, hours, days, and even months. Depending on the options, crontab will act differently:
Let's start by editing
a crontab
file. If you type
crontab -e, you will be in front of your
favorite text editor if you have set the
EDITOR
or VISUAL
environment variable, otherwise Vi will be used. A line
in a crontab
file is made of six
fields. The first five fields are time intervals for minutes,
hours, days in the month, months and days in the week. The sixth
field is the command to be executed. Lines beginning with a
#
are considered as comments and will be
ignored by crond (the program which is responsible for
executing crontab
files). This format is a
little different for the system crontab
, in
/etc/crontab
. There, the sixth field is the
user name that should be used to start the program on the
seventh field. It should be used for administrative tasks only,
and for running jobs of users which exist only to enhance the
system safety (such as an anti-virus user or a user that was
created to run a database server). Here is an example of
crontab
:
# If you don't want to be sent mail, just comment # out the following line #MAILTO="your_email_address" # # Report every 2 days about new images at 2 pm, # from the example above - after that, "retouch" # the "stamp" file. The "%" is treated as a # newline, this allows you to put several # commands in a same line. 0 14 */2 * * find /shared/images \ -cnewer /shared/images/stamp \ -a -iregex ".*\.jpe?g" \ -a -not -regex \ ".*/old/.*"%touch /shared/images/stamp # # Every Christmas, play a melody :) 0 0 25 12 * mpg123 $HOME/sounds/merryxmas.mp3 # # Every Tuesday at 5pm, print the shopping list... 0 17 * * 2 lpr $HOME/shopping-list.txt
There are several ways to specify
intervals other than the ones shown in this example. You can
specify a set of discrete values
separated by commas (1,14,23
) or a range
(1-15
), or even combine both of them
(1-10,12-20
), optionally with a step
(1-12,20-27/2
). Now it's up to you to find
useful commands to put in it!
You may also want to
launch a command at a given day, but not regularly. For example,
you want to be reminded of an appointment, today at 6pm. You run
X, the X11R6-contrib
package is
installed, and you would like to be notified at 5:30pm, for
example, that you must go. at is what you want
here:
$ at 5:30pm # You're now in front of the "at" prompt at> xmessage "Time to go now! Appointment at 6pm" # Type CTRL-d to exit at> <EOT> job 1 at 2005-02-23 17:30 $
You can specify the time in different ways:
now +
<interval>
: means, well, now, plus an
interval (Optional. No interval specified means just now). The
syntax for the interval is <n>
(minutes|hours|days|weeks|months)
. For example, you can
specify now + 1 hour
(an hour from now),
now + 3 days
(three days from now) and so
on.
<time>
<day>
: fully specify the date. The
<time>
parameter is mandatory.
at is very liberal in what it accepts: you can type
0100
, 04:20
,
2am
, 0530pm
,
1800
, or one of three special values:
noon
, teatime
(4pm) or
midnight
. The
<day>
parameter is optional. You can
specify this in different ways as well:
12/20/2001
for example, which stands for
December 20th, 2001, or, the
European way, 20.12.2001
. You may omit
the year, but then only the European notation is accepted:
20.12
. You can also specify the month in
full letters: Dec 20
and 20
Dec
are both valid.
at also accepts different options: