- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- cron job to run on the last business day of the mo...
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
тАО09-20-2019 03:44 AM
тАО09-20-2019 03:44 AM
cron job to run on the last business day of the month
Hi Experts,
I need to schedule a cron job to run on the last "working day" (last business day excluding weekend ) of the month. Please can someone advise me on how to achieve that?
Thanks all
- Tags:
- crontab
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2019 07:44 AM
тАО09-20-2019 07:44 AM
Re: cron job to run on the last business day of the month
I've created a below script and i dont know if this is the right approach. Please can you experts help me to get this to work if this is thre only best way to achieve what im after?
Here I am defining the las working day for all months in the script .
thanks
#!/usr/bin/bash
typeset -i x1_day_of_month=`date +"%d"`
typeset -i x1_month_of_year=`date +"%b"`
typeset -i x1_year=`date +"%Y"`x1_datestring=`$x1_day_of_month $x1_month_of_year $x1_year`
x1_listofdates="
30Sep2019
if [ "$x1_D" = "$x1_$listofdates"] ; then
echo "Run job"elseif [ $x1_FLAGRUN != 1 ] ; then
echo "do not run fancyjob today"
exit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2019 05:59 PM
тАО09-20-2019 05:59 PM
Re: cron job to run on the last business day of the month
> [...] i dont know if this is the right approach. [...]
I would say that running the script every day, and letting it decide
if Today is the right day, is a good approach.
> [...] Here I am defining the las working day for all months in the
> script .
Manually creating a list of dates seems to me to be asking for
trouble. A modern "date" command (with a "v" option) offers many
helpful features.
I don't have an HP-UX system running at the moment, but the following
method seems to work on a Mac:
#!/bin/sh
# Start of test loop.
ofs=1 # Offset (months), for testing.
while test ${ofs} -lt 24 ; do
# Actual last-workday-of-month calculation.
dow=8 # "%u": 1=Mon - 7=Sun.
back=0 # Days back from end-of-month.
while test ${dow} -gt 5 ; do # Want day-of-week Mon-Fri (1-5).
back=` expr ${back} + 1 ` # Increment back step.
date -v1d -v+${ofs}m -v-${back}d # End of this month less "back" days.
# (If "ofs" == 1.)
dow=` date -v1d -v+${ofs}m -v-${back}d +%u ` # Day of week.
echo "dow = ${dow}"
done
# Date strings for comparison with Today.
lwd_dat=` date -v1d -v+${ofs}m -v-${back}d +%G-%m-%d `
tod_dat=` date +%G-%m-%d `
echo ${lwd_dat}
### echo ${tod_dat}
# End of test loop.
echo ''
ofs=` expr $ofs + 1 ` # Increment offset months.
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2019 11:06 PM
тАО09-20-2019 11:06 PM
Re: cron job to run on the last business day of the month
Here's a simpler way:
[[ $(date +%d) != $(cal | awk '{ if(NF>1) a=$NF ; if (NF==7) a=$6}END{print a}') ]] &&
exit
Schedule your script to run every day.
Put this test at the start of your script.
It will always exit if today's date is not the last business day of the month.
Here's a snippet to show the calendar for each month and the last business day of the month:
cal $(date +%Y)
for MON in 1 2 3 4 5 6 7 8 9 10 11 12
do
LASTBUSDAY=$(cal $MON $(date +%Y) | awk '{ if(NF>1) a=$NF ; if (NF==7) a=$6}END{print a}')
echo "$MON/$LASTBUSDAY \c"
done
echo
Looks like this for 2019:
2019
Jan Feb Mar
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
Apr May Jun
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30
Jul Aug Sep
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7
7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14
14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21
21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28
28 29 30 31 25 26 27 28 29 30 31 29 30
Oct Nov Dec
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 5 1 2 1 2 3 4 5 6 7
6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14
13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21
20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
1/31 2/28 3/29 4/30 5/31 6/28 7/31 8/30 9/30 10/31 11/29 12/31
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2019 06:43 AM
тАО09-21-2019 06:43 AM
Re: cron job to run on the last business day of the month
> Here's a simpler way:
And that's why "if this is thre only best way" is seldom a good
question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2019 02:47 AM
тАО09-23-2019 02:47 AM
Re: cron job to run on the last business day of the month
Hi Bill,
thanks for your response. The snippet output is as below bu 3/30 is a sat as well as 9/28 \c?
1/31 \c
2/28 \c
3/30 \c
4/30 \c
5/31 \c
6/29 \c
7/31 \c
8/31 \c
9/28 \c
10/31 \c
11/30 \c
12/31 \c
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2019 04:08 AM - edited тАО09-23-2019 04:18 AM
тАО09-23-2019 04:08 AM - edited тАО09-23-2019 04:18 AM
Re: cron job to run on the last business day of the month
Hi Steven, I am getting the below error. Any advice?
thanks
date: invalid option -- 'v'
Try 'date --help' for more information.
date: invalid option -- 'v'
Try 'date --help' for more information.
dow =
./lastwday_test.sh: line 13: test: -gt: unary operator expected
date: invalid option -- 'v'
Try 'date --help' for more information.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2019 06:31 AM
тАО09-23-2019 06:31 AM
Re: cron job to run on the last business day of the month
> date: invalid option -- 'v'
Hmmm. I did say "A modern "date" command (with a "v" option) [...]".
I assumed that it might be a GNU "date" feature, but I don't see it
there (GNU coreutils), either. GNU "date" does have a "--date" option
which might be able to do similar things. I'd need to investigate
further.
https://www.gnu.org/software/coreutils/manual/coreutils.html#Options-for-date
Aside from building it from source, GNU coreutils for HP-UX should be
available at/near:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2019 07:54 AM
тАО09-23-2019 07:54 AM
Re: cron job to run on the last business day of the month
>> 3/30 and 9/28
Not sure how you got these values.
I show the vaues in my post: 3/29 and 9/30.
Are you using the standard HP-UX shell /usr/bin/sh?
What version of HP-UX are you running?
I've tested this awk procedure on HP-UX 10.20 through 11.31.
Bill Hassell, sysadmin