seo info / cronjob - cron howto

Where are the Cronjob files?

To edit your cron file, simply type crontab -e and the file is opened in the standard editor.

Another way to execute a script on a regular basis is to copy it to the cron directories. There are /etc/cron.daily and /etc/cron.monthly.

Format of the crontab file?

The general formating of the crontab file is:

minute | hour | day of month | month | day of week | command

Each of the fields may contain one or more of the following values:

  • minute: 0-59
  • hour: 0-23
  • day of month: 1-31
  • month: 1-12
  • day of week: 0-7 (where 0 and 7 represent sunday!)
  • command: The command or script you want to run.

Example cronjobs using crontab

0 12 * * * /root/myscript.sh
The would execute the given script a 0 minutes and 12 hours of * every day, * every month and * every day of the week.

More crontab entries

With the above examples it is possible to run script or commands at a certain moment, using a cronjob. But it is not possible to run the script for example every 5 minutes or every second day. Do do that, there are a few more crontab entries. Below are a few examples together with their explanation.

*/5 * */2 * * /root/myscript.sh
This would run a script every five minutes of every hour. But only every second day of every month. This could be every day of the week.

10 3,9,15,21 * * 1-5 /root/myscript.sh
This script would be executed ten minutes after the hour. But only when the hour is 3am, 9am, 3pm or 9pm. This could be any day of the month and any month. But only from the weekdays monday to friday.

Special cronjob calls

There are the following special calls for cronjobs.

  • @reboot Execute a command or script at system boot.
  • @yearly Execute a command of script once a year. This is a shortcut for * * * */12 *.
  • @monthly This runs a command or script once a month.
  • @weekly This is to run a script or command once a week. Like * * * * */7.
  • @daily This would run a command or script once every day.
  • @hourly Finally, this would run a command or script every hour.