Scheduling task on Linux is very important topic to talk about. Cron and its associated commands are designed to regularly execute tasks/jobs. Cron daemon, checks every minute for any tasks that need to be run, checking its files.
Any user (also root) can edit that file to configure any task, typing:
$ crontab -e
note: root can configure cron tasks for any user, typing:
# crontab -e user
A cron entry is normally made up of six fields, for instance:
30 1 * * * /root/myscript.sh
This cron entry runs myscript everyday at 1:30 am.
All fields and their possible values are followings:
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,$
# | | | | |
# * * * * * user-name command to be executed
Alternative options consist of only two fields (date option + command):
For instance: if @weekly = 0 0 * * 0 . Script below will be executed every week on Sunday's midnight:
We can let get a little more complex, running followings cron jobs:
25 5-17 * * * /root/script -> that script will be executed at 25 minutes past the hour, every hour from 5am to 5pm
15 9,12,15 * * * /root/script -> that script will be executed at 15 minutes past the hour, only at 9am, 12am and 3pm.
Finally, I'm going to show you some tricks using /:
/20 14,16 * * * /root/script -> That entry run the script every 20 minutes during the hours of 14 and 16 (starting at the top of the hour each time). It's say, that occurs at 20 minutes after the hour but only once an hour.
0 1/2 * * * /root/script -> That script will start at 1am and specifies every other hour: 1am, 3am, 5am, 7am and so on.