You’re know, Crontab will not work on a specific day of the month but it can be a specific minute, hour, month and day of the week. So, if you need to run some command in the first day of month or last day of month you can’t use crontab to scheduled it. However, you can use crontab with shell script to filter out a particular date that you need to run the command.
Example script to run on last day of the month
- Create lastdayofmonth.sh via terminal by running
[root@Ezylinux ~]# vi /root/lastdayofmonth.sh
#!/bin/bash TODAY=`/bin/date +%d` TOMORROW=`/bin/date +%d -d "1 day"` # See if tomorrow's day is less than today's if [ $TOMORROW -lt $TODAY ]; then COMMAND HERE fi exit 1 - Add schedule for this script
[root@Ezylinux ~]# crontab -e
1 0 * * * /root/lastdayofmonth.sh
Example script to run on first day of the month
- Create 1stdayofmonth.sh file by running
[root@Ezylinux ~]# vi /root/1stdayofmonth.sh
#!/bin/bash TODAY=`/bin/date +%d` YESTERDAY=`/bin/date +%d -d "-1 day"` # See if tomorrow's day is less than today's if [ $TODAY -lt $YESTERDAY ]; then COMMAND HERE fi exit 1 - Add schedule for this script
[root@Ezylinux ~]# crontab -e
1 0 * * * /root/1stdayofmonth.sh
Incoming search terms: cron last day of month


December 14th, 2011
Tum.
Posted in
Tags: