Introduction
It is often required to perform certain tasks at specific intervals like cleaning the server caches, updating your blogs or running server side security checks. Linux offers a wonderful utility called cron to perform such scheduled tasks without any user interactions. With cron, you will be able to execute tasks using most of the shell commands, ignore or output or send the results to your configured email id.
Adding Cron Jobs
Cronjobs can provide you a time accuracy of 5 points,
Minute of the hour 0 – 60
Hour of the day 0 – 23
Day of the month 1 – 31
Month of the year 1 – 12
Day of the week 0 – 7
The syntax of a cron job is as shown below.
MIN HOUR DOM MON DOW CMD
CMD represent the command that you want to execute in that specific cron schedule.
A sample cronjob to run on every tuesday 2.30am every month will be like shown below.
30 2 * * 2 CMD
* is used to represent all for that corresponding field.
Service Options.
Options:
-u <user> define user
-e edit user’s crontab
-l list user’s crontab
-r delete user’s crontab
-i prompt before deleting
Tips
List users cronjobs:
crontab -l -u <user_name>
Edit users cronjobs:
crontab -e -u <user_name>
List user cronjobs from the user’s shell:
crontab -l
Edit user cronjobs from the user’s shell:
contab -e
Clear all cronjobs for user:
crontab -r
Cronjob to run every 2 hour:
0 */2 * * *
Please use the / character to specify the repetition interval */5 for every 5, can be used in any fields like minute hour day etc
Cronjob to run 2,3,4,5,6,7 hours
0 2-7 * * *
Please use the – character to specify the range, 2-7 is same as 2,3,4,5,6,7, can be used in any fields like minute hour day etc
Cronjob to run every minute
* * * * *