Using Crontab on Linux

The use of crontab in Linux is almost vital, however many users know nothing of how it works, or what it is used for. Crontab is basically a scheduler which is used to run scripts at intervals. Possibilities of usage are

a) logrotation
b) backups
c) time synchronization

Crontab can he extremely handy, if you know how to use it ;-) Each line consisting of 6 fields separated by spaces.

Fields:

00,15,30,45   00-23/1   *   *   mon,tue,wed,thu,fri   /root/backup_script
  1. minute of the hour, 00 to 59
  2. hour of the day, 00 to 23 (military time)
  3. day of the month, 1 to 31
  4. month of the year, 1 to 12
  5. day of the week, sun, mon, tue,....
  6. actual command to execute

* an asterisk that matches all possible values,
* a single integer that matches that exact value,
* a list of integers separated by commas (no spaces) used to match any one of the values
* two integers separated by a dash (a range) used to match any value within the range.

 

Manual Edit

To edit your own crontab:

crontab -e 

 

Crontab Import

You can also import a crontab file (written exactly in the same form):

crontab ^^^^ 

 

Use special string to save time

Instead of the first five fields, you can use any one of eight special strings. It will not just save your time but it will improve readability.

Special stringMeaning
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".

Run ntpdate every hour:

@hourly /path/to/ntpdate