Scheduling Cronjobs with Multiple Examples(Automation)
cronjob scheduling is possible using crontab command and each and every user crontab is separate
# crontab -e = To edit cronjob schedule
# crontab -l = To list scheduled cronjobs
# crontab -r = To remove all cronjobs in single attempt
whenever you schedule a cronjob with particular user the same cronjob will run using same user, ensure user is having execution permission to the user.
Know crontab fields and its values
[root@server ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# 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,fri,sat
# | | | | |
# * * * * * user-name command to be executed
Scheduling cronjobs to run @every minute
To run cronjob yet every one minute first field is minutes, first field we can right with separated by comma. below three examples we can use to schedule a cronjob to run every minute.
# crontab -e
* * * * * sh /scripts/everyminute.sh
1,2,3,4,5,6,7,8,9,10......60 * * * * sh /scripts/everyminute.sh
*/1 * * * * sh /scripts/everyoneminute.sh
Scheduling cronjobs to run @every 5 minutes
below two examples to schedule cronjob for every 5 minutes, again we have to make use of first field to run script @every 5 minutes because first field is minutes
*/5 * * * * sh /scripts/testscript.sh 5,10,15,20,25,30,35,40,45,50,55,0 * * * * sh /scripts/testscript.sh
Scheduling cronjobs to run @every 30 minutes if we want monitoring our file system @every 30 minutes using shell script you can do using below
* /30 * sh /scripts/FS-Monitoring.sh or 0,30 * * * * sh /scripts/FS-Monitoring.sh
Scheduling cronjobs to run @every hour
0 */3 * * * sh /scripts/cronjob-every-3hours.sh or * 0,3,6,9,12,15,18,21 * * * sh /scripts/cronjob-every-3hours.sh
Scheduling cronjobs to run @every day Or once in a day
59 23 * * * sh /scripts/daily-report.sh or @daily sh /scripts/daily-report.sh
Schedule cronjobs to run every alternate day
0 * * * 0,2,4,6 sh /scripts/every-alternate-day.sh
Run cronjob first and Second Saturday of the month
0 1 1-7,15-21 6 /scripts/every-first-second-sat.sh
9. Cronjob for every week
to schedule a cronjob to run @every week we can make use of week of the day 5th field in crontab
0 0 * * 0 sh /scripts/every-week.sh
OR
we can also make use of special schedule
@weekly sh /scripts/every-week.sh
10. Run cronjob @every month 1st date
Here we have to use day of the month, field 3
0 0 1 * * sh /scripts/every-mont-1st.sh
OR
@monthly sh /scripts/every-month.sh
11. Schedule cronjob to run once in a year
I would like to schedule an cronjob to say happy new year to all the employees on midnight of every year 31st Dec.
59 23 31 12 * echo "Happy New Year to All"
OR
@yearly echo "Happy New Year"
12. I want to run a cronjob @every server reboot
every time whenever server is rebooted i would like to get notification. We can make use of special schedule
@reboot echo Server Rebooted | mail -s "Server Rebooted `hostname`"