Commands Startup Scheduling

crontab: reporting or editing your crontab file

crontab is a command which 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:

  • -l: Print your current crontab file.

  • -e: Edit your crontab file.

  • -r: Remove your current crontab file.

  • -u <user>: Apply one of the above options for user <user>. Only root can do this.

Let's start by editing a crontab. 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 to be comments and will be ignored by crond (the program which is responsible for executing crontab files). Here is an example of crontab:

Note

in order to print this out in a readable font, we had to break up long lines. Therefore, some chunks must be typed on a single line. When the \ character ends a line, this means this line is to be continued. This convention works in Makefile files and in the shell as well as in other contexts.

# If you don't want to be sent mail, just comment 
#   out the following line
#MAILTO=""
#
# 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. For 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!

at: schedule a command, but only once

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, and you'd 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>
$

You can specify the time in different manners:

  • now +<interval>: Means, well, now, plus an interval (optionally. 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 for example 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 it in different manners 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 or 20 Dec are both valid.

at also accepts different options:

  • -l: Prints the list of currently queued jobs; the first field is the job number. This is equivalent to the atq command.

  • -d <n>: Remove job number <n> from the queue. You can obtain job numbers from atq. This is equivalent to atrm <n>.

As usual, see the at(1) manpage for more options.