If we need to run a specific task repetitively, then the solution is to use crontab.
The syntax of the command is as follows:
$ crontab –e
This will open a new editor. The following diagram is the syntax to add tasks. The fields to use for repeating tasks at what time are explained here:

Finally, to save the jobs, use the following:
wq # save and quite crontab job
The following are a few examples of the crontab command:
5 * * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
15 14 1 * * * $HOME/bin/monthly
<ganesh@abc.com>:
0 22 * * 1-5 sendmail ganesh@abc.com < ~/work/email.txt
sendmail utility is used for sending e-mails. We can use the mail utility also as follows:
sendmail user@example.com < /tmp/email.txt
23 0-23/2 * * * echo "run 23 minutes after midn, 2 am, 4 am, everyday 5 4 * * sun echo "run at 5 after 4 every Sunday"
The following are a few more crontab command examples:
|
Min |
Hour |
Day / month |
Month |
Day / week |
Execution time |
|---|---|---|---|---|---|
|
45 |
0 |
5 |
1,6,12 |
* |
00:45 hrs on the 5th of January, June, and December. |
|
0 |
18 |
* |
10 |
1-5 |
6.00 P.M. every weekday (Monday-Friday) only in October. |
|
0 |
0 |
1,10,15 |
* |
* |
Midnight on the 1st ,10th, and 15th of the month |
|
5,10 |
0 |
10 |
* |
1 |
At 12.05 and 12.10 every Monday and on the 10th of every month |
We can add macros in the crontab file. Use the following to restart my_program after each reboot:
@reboot /bin/my_program @reboot echo `hostname` was rebooted at `date` | mail -s "Reboot notification" ganesh.admin@some-corp.com
The following is the summary of a few more macros:
|
Entry |
Description |
Equivalent To |
|---|---|---|
|
@reboot |
Run once at startup |
None |
|
@weekly |
Run once a week |
0 0 * * 0 |
|
@daily |
Run once a day |
0 0 * * * |
|
@midnight |
(same as @daily) |
0 0 * * * |
|
@hourly |
Run once an hour |
0 * * * * |