Cron is an linux utility to schedule jobs (commands or shell scripts) to automate system maintenance and/or administration.
The configuration file is located in “/var/spool/cron/username”. The command “crontab –e” will work like opening and editing the file in “vi” editor.
Listing cron jobs for the currently logged in user | crontab –l |
display the cron jobs set under another user | crontab –l –u username |
edit the cron jobs for the currently logged in user | crontab –e |
to modify the cron job of another user | crontab –e –u username |
to remove the cron jobs of currently logged in user | crontab –r |
to remove the cron jobs of another user | crontab –ir –u username |
Scheduling a job in crontab
The syntax of a crontab entry is as follows,
Minute Hour DOM Month DOW Command_to_be_Executed
Minute => In which minute the job to be executed.
Hour => In which Hour the job need to be executed.
DOM => The Day Of Month in which the job need to be executed
Month => In which month the job need to be executed
DOW => The Day of Week in which the job need to be executed.
Schedule a cron job to run on May 23 10.30 am | 30 10 23 05 * sh /root/sh |
Schedule a job to run in every minute | * * * * * sh /root/test.sh |
Schedule a cron job to run in every 10 minutes | */10 * * * * sh /root/test.sh |
Schedule a cron job to run in every 5 minutes on Monday through Friday | */5 * * * 1-5 sh /root/test.sh |
Schedule a cron job to run at “3.00 pm” on every Thursday | 00 15 * * 4 sh /root/test.sh |
Sending report to a mail address
You can setup a mail address on which you want to receive the notifications of executed cron jobs as follows.
MAILTO=your@email.com
If you want to disable sending output mail, you can set it as follows.
* * * * * sh /root/test.sh > /dev/null 2>&1
Where 2 refers to “STDERR” (errors) and 1 refers to STDOUT (standard output). So “2>&1” will make both errors and output to be created in one data stream. Hence the datastream (both errors and output) will be send to /dev/null a special device file which will discard all data written to it without notification.
If you want to discard output mails but require mail to be sent in case of errors, it need to be set as follows.
* * * * * sh /root/test.sh > /dev/null