Saturday, June 9, 2012

Crontab [classic]

Crontab fields
Here is a typical crontab entry and the definition of cron fields:
00 03 * * * /tmp/zing.sh
  1. minute (00-59)
  2. hour (00-23)
  3. day of the month (01-31)
  4. month of the year (01-12)
  5. day of the week (0-6 with 0=Sunday)
  6. program or command to run
An asterisk (*) in any field means run every time for this field.
Ranges (X-Y) and steps (X-Y/Z) can also be defined.

Special @reboot option
Recent versions of cron support @reboot instead of time and day settings to run a command after boot. For example:
@reboot /usr/bin/command-to-run

I am not sure if I like this option, compared to using system start up scripts, like /etc/rc.d/rc.local.

User crontabs (including the root user)
To edit a user crontab (including root):
crontab -e
To delete a crontab:
crontab -r

User crontabs are stored as text files in /var/spool/cron/.

System crontab
The system crontab is stored in /etc/crontab. It can be changed (as root) with a text editor.
The system crontab has one extra field before the program to run, which is the user to run the command as (usually root).

2 comments:

  1. Thanks for sharing the article. You can find more examples about crontab at crontab command examples in unix

    ReplyDelete
  2. vijay,

    I always have to reference something when I am doing something odd, like day of the month jobs, etc.

    Bonus tip, when cron jobs start, they don't get a full shell environment so you can't rely on the $PATH being there. For that reason, it is always a good idea to put the full path to all programs and files in the command, or if you are running a script, set the environment variables in the script.

    Keith

    ReplyDelete