- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- scheduling a cron job on the last day of the month
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2003 04:33 AM
тАО10-22-2003 04:33 AM
I did solve in round about way.Like to know the best way.
Singaram
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2003 04:39 AM
тАО10-22-2003 04:39 AM
Re: scheduling a cron job on the last day of the month
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2003 04:42 AM
тАО10-22-2003 04:42 AM
Re: scheduling a cron job on the last day of the month
Note: Cron has a very limited PATH so make sure that whereever you install the attached caljd.sh, the first thing that your cron script does is sets and exports PATH
#!/usr/bin/sh
export PATH=/usr/bin:/usr/local/bin
if [[ $(caljd.sh -M) -ne $(caljd.sh -n 1 -M) ]]
then
echo "Last day of month; your commands go here"
fi
This compares the current date's month with the next days (-n 1). If they differ then it's the last day of the month.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2003 07:36 AM
тАО10-22-2003 07:36 AM
Re: scheduling a cron job on the last day of the month
As per Pete's suggestion:
TZ=PST8PDT-24 date +%d | read DOM
if [ "$DOM" -eq 1 ]
then
do your thing
fi
TZ=PST8PDT-24 date +%d | read DOM==> says DOM is equal to the day of the month following today.
Best of luck.
Regards,
dl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2003 07:55 AM
тАО10-22-2003 07:55 AM
Re: scheduling a cron job on the last day of the month
I hereby quote Craig Rants from Favourite Sysadmin Scripts:
There is not much to it, but it always solves the problem of running a script on the last day of the month. Put 28-31 (for days) in your crontab and put this little section in for date determination.
#!/bin/sh
if test `TZ=MET-24 date +%d` = 01
then
exec command
else
exit 1
fi
unqoute.
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2003 09:23 AM
тАО10-22-2003 09:23 AM
SolutionI always like playing with hacks like this
that use the 'cal' command to get the answer.
Something like this could work:
#!/bin/sh
# lastday.sh
TODAY=$(date +\%d)
LASTDAY=$(cal | tail -2 | head -1 | awk '{print $NF}')
if [[ $TODAY = $LASTDAY ]];
then
echo "Last day of the month."
else
echo "Not the last day of the month."
fi
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2003 09:38 AM
тАО10-22-2003 09:38 AM
Re: scheduling a cron job on the last day of the month
I like yours as it is very simple.
But still i dont like the idea of running the script every day. Having found the last day cant we add the cron job for the last day thru a script.
Thanks again
Singaram
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2003 07:49 PM
тАО10-22-2003 07:49 PM
Re: scheduling a cron job on the last day of the month
Like John Korteman suggested, you do not have to run it every month day, only the last couple, starting the 28th:
so it runs maximum 4 days per month, for a short while..
p.s. would be nive, if you gave points to the solutions you appreciate..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2003 07:55 PM
тАО10-22-2003 07:55 PM
Re: scheduling a cron job on the last day of the month
you don't need to run it every day, it will do if you run it at the 28th till 31th of every month.
Volkmar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-23-2003 01:01 AM
тАО10-23-2003 01:01 AM
Re: scheduling a cron job on the last day of the month
I want to run a cron job only on the first of the month, which finds out the last day of the month and should automatically schedule the month end job on the last day.
Now my question is "Is there a way to schedule a cron job automatically from a script?"
Thanks to all
Kasper haitsma
I will assign points as soon as i conclude this discussion.
Singaram
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-23-2003 01:26 AM
тАО10-23-2003 01:26 AM
Re: scheduling a cron job on the last day of the month
I'm not good in shell scripting, it should be possible, but you have to rewrite your crontab file '/var/spool/cron/crontabs/root' every month:
At the first day you have to check (with cron) which month you have. Then set the last day (for January, March, May ... 31th for example), then read you crontab file, delete (for example) the last line and add a new one with the current 'last day'.
Then write the new crontab file.
Good luck
Volkmar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-23-2003 01:28 AM
тАО10-23-2003 01:28 AM
Re: scheduling a cron job on the last day of the month
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-23-2003 01:28 AM
тАО10-23-2003 01:28 AM
Re: scheduling a cron job on the last day of the month
Or use crontab -l |
The commands to change the script line would become something like this:
awk '/<scriptname>/ { $5='$LASTDAY'}
{print}'
As long as LASTDAY contains the last day of the month like in JP's script, it work.
Of course there are cleaner ways to do it, but is should work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-23-2003 01:35 AM
тАО10-23-2003 01:35 AM
Re: scheduling a cron job on the last day of the month
To do it your way, run the cron job on the first of the month to calculate the end of the month day (let's call it $EOM, "Oct 31, for example). Then invoke an at job to run your end of month job thusly:
at -f /full/path/of/your/job $EOM
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-23-2003 07:16 AM
тАО10-23-2003 07:16 AM
Re: scheduling a cron job on the last day of the month
I have to find the last day of the month avoiding saturday and sunday.
Any short cuts on awk?
Thanks
Sing
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-23-2003 07:29 AM
тАО10-23-2003 07:29 AM
Re: scheduling a cron job on the last day of the month
if [[ $(caljd.sh -M) -ne $(caljd.sh -n 1 -x 6 -x 0 -M) ]]
then
echo "Do your thing"
fi
That says compare today's month with tomorrow's month (-n 1) unless that day falls on a Saturday -x 6 or a Sunday -x 0. This has the effect of running either on the last day of the month or on the last Friday.
Want to get more complicated? Add -h and it will skip holidays as well if your /etc/acct/holidays is setup. Invoke as caljd.sh -u for full usage.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-23-2003 07:57 AM
тАО10-23-2003 07:57 AM
Re: scheduling a cron job on the last day of the month
My little hack with 'cal' will find the last day of the month, but if you need it to be a weekday the hack gets very tricky.
Probably the easiest thing for you to do is to use Clay's script. His script handles all kinds of date calculations and is very handy to have in your kit.
JP