Friday, December 21, 2001

Cron Job

Cron Fromat Minute,Hour,Date,Month,Day


You can use commas (,) to put more than one time specification in a field. There are other shortcuts, better explained with examples:

  1. To run a job every hour at 0 and 30 minutes:
    0,30 * * *
  2. To run a job at 1AM every Sunday:
    0 1 * * Sun
  3. To run a job every five minutes:
    0-55/5 * * * *
  4. To run a job the first of January at 4PM:
    0 16 1 Jan *
  5. To run a job weekly you can use:
    @weekly

    which is equivalent to
    0 0 * * 0

    Here the last 0 means on Sunday. You can also use other special strings: @yearly, @annualy, @daily, @monthly, @midnight, @hourly (see the manual page with man 5 crontab, that is, a manual page in section 5).

    Now that we hav a little understanding of times we can use cron. This is called with the command crontab -e to edit your cron jobs. A small warning: this command will put you in an editing mode, calling some default editor. If you do not like the editor (quite likely to be vi) you can change it. For example, to use emacs in a bash shell you can do this:

    EDITOR=emacs crontab -e

Once you are in the editing mode you enter your times and the commands to be executed. The commands could be just one simple command, a series of them separated by semicolons (;) or the name of a script. Quitting your editor will save your cron job.

The output of cron, if any, might be mailed to your account. Or you can redirect output to some file (more on this later).

To see what cron jobs are in your account do this:

crontab -l

To delete your cron jobs:

crontab -r

You can use crontab -e to edit existing cron jobs.

Tuesday, December 04, 2001

Executing Commands at a Future time

Some times you might want to execute a command at a certain (future) time where you are not logged on in your computer. The program at allows you do to precisely this. The usage of this command is simple, you give a time and a series of commands to be executed, and when the time comes the system will do what you want. Only thing you have to be careful is that the output of some commands might be lost (although in many system you get an email with the output, if that output should have been written to your terminal/console). But if we forget that for the time begin (I will post later how to redirect output of commands to files) we can start our first trials with the at command.

First of all, how to call at. You have to give a time at which you want your commands to be executed. Here are a few examples (you can find more details in the manual page, man at); most examples are self-explanatory:

  1. at 4pm will execute at 4 in the afternoon/evening today if you give the at command before 4PM, tomorrow otherwise. An equivalent time is at teatime (there are other short names: midnight and noon)
  2. at 10:31am
  3. at 10:30pm Jul 31
  4. at 4pm + 3 days will execute at 4 afternoon/evening three days from today
  5. at 4pm 101005 will execute on the 10th of October of 2005

Once you have given the at command with a time you will get a prompt that looks like this: at> Enter here the commands you want to be executed.

To look at what jobs are waiting in the at queue you can run the program atq. The output will look like this:

4       Wed Sep 28 14:00:00 2005 a pablo

This means thre is a job to be executed at 2PM. The job id is 4.

If you want to remove a job you have to type its id. In the above example we could do:

atrm 4

In a future post I will explain how to use cron to run commands periodically.