Using crontab on Linux

Dec 23, 2012 System

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

  • log rotation
  • backups
  • 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

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
@rebootRun once, at startup.
@yearlyRun once a year, “0 0 1 1 *”
@annually(same as @yearly)
@monthlyRun once a month, “0 0 1 * *”
@weeklyRun once a week, “0 0 * * 0”
@dailyRun once a day, “0 0 * * *”
@midnight(same as @daily)
@hourlyRun once an hour, “0 * * * *”

Run ntpdate every hour:

@hourly /path/to/ntpdate

Comments