Script to run on 2 4 and 7 day of the month

I am looking for a unix script which could run a job on 2, 4 and 7 working day of the month.

if the days are falling on the saturday/sunday. it should run on the next day.

Thank you..

Please give way more detail. How can the 2./4./7. WORKING day fall on a Sunday?

cron will schedule jobs according to a time pattern up to you. If that is not doable/sufficient, you'll need to add time control logics to you script.

our requirement is that we need to create a touch file on 2 4 and 7 th working day of the each month ...

If the days falls on saturday/sunday, the touch file should be created on next working day.

Hi.

It seems that we need to create a list of the working days in a month, then select days 2,4,7 from that list and check if today is in that list. Here is a demonstration of those steps, using utilities that are found in GNU/Debian as noted:

#!/usr/bin/env bash

# @(#) s3       Demonstrate display of days in month: "working", not weekend.

D=dateutils.dseq
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C $D date grep

pl " First day of current month:"
date --date=today +%Y-%m-01

pl " Day 30 days in the future:"
date --date="now + 30 days" +%F

# Get list of working days from first of month.
$D -s "ss" $( date --date=today +%Y-%m-01 ) "$( date --date="now + 30 days" +%F )" -f "%F %a" > f1

pl " Sample of output:"
head -8 f1

pl " Select days 2,4,7:"
sed -n -e '2p' -e '4p' -e '7p' f1 |
tee f2

pl " Test if today is in the 3-day list:"
today=$( date +%F )
pe " today is $today"

if grep "$today" f2 >/dev/null
then
  pe " YES, touch the file now."
else
  pe " Today is NOT day 2,4,7, skipping touch."
fi

exit 0

producing (luckily today seems like the 7th working day):

$ ./s3

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.4 (jessie) 
bash GNU bash 4.3.30
dateutils.dseq dseq 0.3.1
date (GNU coreutils) 8.23
grep (GNU grep) 2.20

-----
 First day of current month:
2016-06-01

-----
 Day 30 days in the future:
2016-07-09

-----
 Sample of output:
2016-06-01 Wed
2016-06-02 Thu
2016-06-03 Fri
2016-06-06 Mon
2016-06-07 Tue
2016-06-08 Wed
2016-06-09 Thu
2016-06-10 Fri

-----
 Select days 2,4,7:
2016-06-02 Thu
2016-06-06 Mon
2016-06-09 Thu

-----
 Test if today is in the 3-day list:
 today is 2016-06-09
 YES, touch the file now.

Not trivial, but an interesting problem, however, end cases are not tested, so some work may be needed to turn these ideas into a production script.

See man pages for details.

Best wishes ... cheers, drl

Great work ... Thank you mate,

getting this error

+ date '--date=now + 30 days' +%F
2016-07-09
++ date --date=today +%Y-%m-01
++ date '--date=now + 30 days' +%F
+ dateutils.dseq -s ss 2016-06-01 2016-07-09 -f '%F %a'
./ra_test.sh: line 27: dateutils.dseq: command not found
+ pl ' Sample of output:'
+ pe
+ printf '\n'

Install date-utils?

apt-get install date-utils

is there any other way to over come it without installing the data utility ..

Hi.

First of 17 million hits from Google search for linux sequence of dates display OR compute: unix - How to generate a sequence of dates given starting and ending dates using AWK of BASH scripts? - Stack Overflow

You'll need to do work on those selections to create a list similar to that from dateutils.dseq .

Best wishes ... cheers, drl

we are currently on Redhat linux 6.5..The command apt-get install date-utils asked for execution is for Ubuntu Linux.

Hello tradingspecial,

Could you please check following script and let me know if this helps you.

cat script.ksh
PWD=`pwd`
OUT_FILE=$PWD"/output_file"
ZERO=0
if [[ -f $OUT_FILE ]]
then
        echo "Run script here."    ####You could call you script/command here.
        rm $OUT_FILE
        exit;
fi
VAL=`cal 12 2016 | awk -vout_file="$OUT_FILE" 'NR>2{for(i=1;i<=NF;i++){if(($i==2 || $i==4 || $i==7) && (i==7 || i==1)){print $i " of this month is falling on a working day." > out_file;print 1} else {print 0}}}'`
 
if [[ $VAL == $ZERO ]]
then
        echo "Run script here."   ####You could call you script/command here.
fi

So following things also should be done by you apart from above code.
i- Create a script(as above shown) with proper permissions.
ii- Schedule an entry in cron by doing crontab -e .
iii- Entry should be like that so it should run that script for each month from dates 1 to 8.
It could be like as follows, where I am running script from each month's 1st to 8th date on 1 AM of your box timing.

0 1 1-8 * * /path/to/exact/script.ksh

iv- Also in above code I have tested it with command cal 12 2016 because it has 2nd of October falling on Sunday, you could change it to cal in above code.
A big disclaimer here, I haven't tested it at all, this goes to your plate, please do proper testing and get back to us then.

Thanks,
R. Singh