How to set a crontab to run command on the last day of every month.
Linux crontab specifies execution times, days, and months, but cannot directly select the last day of a month or its first Saturday. A script can add that control. This article provides examples of scripts used with crontab to choose execution dates more precisely.
Example script to run on the last day of the month
Create lastdaymonth.sh:
[root@Ezylinux ~]# vi /root/lastdaymonth.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 //ใส่คำสั่งที่ต้องการให้ทำงาน
fi
exit 1
Create the schedule:
[root@Ezylinux ~]# crontab -e
1 0 * * * /root/lastdaymonth.sh
Example script to run on the first Saturday of the month Create 1stSat.sh:
[root@Ezylinux ~]# vi /root/1stSat.sh
#!/bin/bash
DAY=`date +%d`
if (($DAY <= 7)) ; then
COMMAND //ใส่คำสั่งที่ต้องการให้ทำงาน
fi
exit 1
Create the schedule:
[root@Ezylinux ~]# crontab -e
1 0 * * 6 /root/1stSat.sh