I have to schedule a job every last day of the month. I'm using crontab for several jobs, so I want to schedule this job also via cron to have one schedule program.
In short my question.
How can I determin if today is the last day of the month?
Thanks for the input. But (there is always a but). It is nog possible to run the command on 1 o'clock on the 1st of the next month.
The other suggestion are "work-around", because I still have to think about which year it is for the month februari. The system administrator who will operate this system will "forget" to change the contab (or script) when we have a special year!
I'm looking for a command that tells me what day it is tomorrow. Is tomorrow the 1st of the next month then I know for sure that today is the last day of the month... I don't have to check/update the crontab...
It's not that difficult to find other method to calc the last day, mine was just idea.
Take a look at cal
or
Solution 1 (using date, change MET to your local time zone)
#!/bin/sh
if test `TZ=MET-24 date +%d` = 1; then
# today is the last day of month
fi
You can call this script from cron, say with a crontab of
4 2 28-31 * * /path/to/your/script
Solution 2 (more generic, using perl5)
#!/usr/bin/perl -w
#
# last-day-of-month - check if today is the last day of a month
#
# Input: none.
# Output: none.
# Exit status: 0 (true) if today is the last day in a month, otherwise 1.
# Algorithm: Get localtime and advance the day of month by one. Let mktime
# normalize the result and check whether day of month became 1.
# Requires: perl5.
#
use POSIX;
@the_time = localtime (time);
++$the_time[3]; # Element 4 is the day of month [1..31]
if ((localtime (POSIX::mktime (@the_time)))[3] == 1) {
exit 0;
}
exit 1;
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.