1846733 Members
3636 Online
110256 Solutions
New Discussion

caljd script

 
SOLVED
Go to solution
Norman_21
Honored Contributor

caljd script

I want to deremine 90 days from now to remind myself to do staff using caljd.sh script. How is the format?

Determine the month 3 days from now unless that falls on a weekend; output in full monthname format:
JD=$(caljd.sh -n 90 -x 6 -x 0 -M -O)

Is this correct?
Can I run it in a cron daily?

10 points for a good answer

Thanks
"Attitudes are contagious, is yours worth catching"/ My first point was given by SEP on January 31, 2003
7 REPLIES 7
Norman_21
Honored Contributor

Re: caljd script

that was from the -u help list. I changed it from 3 days to 90 days. Still want to confirm, if this is correct?

"Attitudes are contagious, is yours worth catching"/ My first point was given by SEP on January 31, 2003
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: caljd script

If you are going to run it from cron you need to do a little more because cron's environemtn is intentionally sparse. You create a wrapper script that is invoked via cron. Something like this (assuming caljd.sh is installed in /usr/local/bin):

#!/usr/bin/sh

PATH=${PATH}:/usr/bin:/usr/local/bin
export PATH

CAL90=$(caljd.shi -S "/" $(caljd.sh -n 90))
echo "90 days from now is ${CAL90}."

Now if you want 90 days from today but if that lands on a Saturday (-x 6) or Sun (-x 0) skip to the next day then:

CAL90=$(caljd.sh -S "/" $(caljd.sh -n 90 -x 6 -x 0))
echo "90 days from now is ${CAL90}."

If you keep your /etc/acct/holidays file current, you can even add -h and it will also skip past holidays.

CAL90=$(caljd.sh -S "/" $(caljd.sh -n 90 -x 6 -x 0))
echo "90 days from now is ${CAL90}."

The inner caljd.sh takes today's date and converts it into a Julian Day (number of days since 4713BCE. The outer caljd.sh then takes this Julian Date and converts it to mm/dd/yyyy.

Your approach would print out the Monthname (e.g. September) 90 days from today.
If it ain't broke, I can fix that.
Norman_21
Honored Contributor

Re: caljd script

I'm honored to have you in my thread!!
Most definitely, I am going to write a small script to ivoke your script!
10 points for you Sir!!!
"Attitudes are contagious, is yours worth catching"/ My first point was given by SEP on January 31, 2003
Norman_21
Honored Contributor

Re: caljd script

Clay,

I've seen your thread:

if [[ $(caljd.sh -M -n 1 -x 6 -x 0) -ne $(caljd.sh -M) ]]
then
echo "Last working day of the month; do your thing"
else
exit 0
fi

So in my case, it'll be something like this:

if [[ $(caljd.sh -n 90 -x 6 -x 0) -ne $(caljd.sh ) ]] > whatever
then
mailx -s " It's been 90 days now..." email@domain.com < whatever
else
exit 0
fi

Any corrections?
"Attitudes are contagious, is yours worth catching"/ My first point was given by SEP on January 31, 2003
A. Clay Stephenson
Acclaimed Contributor

Re: caljd script

Your script:

if [[ $(caljd.sh -n 90 -x 6 -x 0) -ne $(caljd.sh ) ]] > whatever
then
mailx -s " It's been 90 days now..." email@domain.com < whatever
else
exit 0
fi

No, this is not a valid use for caljd.sh. You are essentially asking if (today + 90 days) <> today. That question will always be true.

A much better approach would be to create a script something like this using the at command:
--------------------------------------
#!/usr/bin/sh

PROG=${0}
PATH=${PATH}:/usr/bin/:/usr/local/bin
export PATH

mailx -s "It's been " < whatever
at -f ${PROG} now + 90 days
--------------------------------------

This will reschedule the same script 90 days from now after sending mail.

A slight variation if you wanted to skip weekends and holidays would be (and now we will use caljd.sh):

--------------------------------------
#!/usr/bin/sh

PROG=${0}
PATH=${PATH}:/usr/bin/:/usr/local/bin
export PATH

typeset -i DT90A=$(caljd.sh -n 90)
typeset -i DT90B=$(caljd.sh -n 90 -x 0 -x 6 -h)
typeset -i DIFF=0
typeset -i DAYS=90
DIFF=$(( ${DT90B} - ${DT90A} ))
(( DAYS += ${DIFF} ))

mailx -s "It's been " < whatever
at -f ${PROG} now + ${DAYS} days
--------------------------------------

The idea is that is today + 90 days lands on a weekend or holiday we need to skip to the next excluded day; otherwise ${DIFF} = 0 and ${DAYS} remains at 90.

Man at for details.

If it ain't broke, I can fix that.
Norman_21
Honored Contributor

Re: caljd script

Clay,

Let's not worry about the cron and the holidays + weekends.
All I wanted is to use your script to execute a command every 90 days. Can this be done using your script caljd.sh

Cron job can do that I know, but I wanted to use your script as a counter to run some commands every 90 days?

Many thanks


"Attitudes are contagious, is yours worth catching"/ My first point was given by SEP on January 31, 2003
A. Clay Stephenson
Acclaimed Contributor

Re: caljd script

No. This is really a perfect fit for at. The idea is that your script runs and then the last thing it does is reschedule itself to run now + 90 days and you are done.
If it ain't broke, I can fix that.