1827444 Members
6101 Online
109965 Solutions
New Discussion

Cron Question

 
f. halili
Trusted Contributor

Cron Question

How do we format this this in the crontab file?

Run "/scripts/backup" every last friday of the month at 6:00am ??

Thanks,
f.halili
derekh
13 REPLIES 13
Steven E. Protter
Exalted Contributor

Re: Cron Question

The tricky part is the last friday of the month.

What you should do is run a script every friday and use caljs.sh, an A. Clay Stepenson invention to determine if it is ineed the last friday of the month and go forward with the backup.

Shell version:
http://www.hpux.ws/merijn/caljd-2.23.sh

Perl version
http://www.hpux.ws/merijn/caljd-2.2.pl

crontab entry:

* * * * 5 /command

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James A. Donovan
Honored Contributor

Re: Cron Question

Well....there are only twelve last Friday's of the month each year.

Put each one on a different line (12 lines in all)
Remember, wherever you go, there you are...
A. Clay Stephenson
Acclaimed Contributor

Re: Cron Question

Using the aforementioned caljd.sh script this is easy. You run you cronjob every Friday and the script itself determines if this is the last Friday by seeing if today and 7 days from now are in a different month. Your should set and export PATH in your script to include the location of caljd.sh or use full pathnames:

#!/usr/bin/sh

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

if [[ $(caljd.sh -M) != $(caljd.sh -n 7 -M) ]]
then
echo "Do your thing; it's the last Friday"
fi
If it ain't broke, I can fix that.
Nguyen Anh Tien
Honored Contributor

Re: Cron Question

This is only this you need
#crontab -e
# Minute Hour MonthDay Month Weekday Command
# ----------------------------------------------------------
0 06 * * 5 /sbin/sh your_path/script


Notes: 0=Sunday, 1=Mon ...5 =Friday
tienna
HP is simple
Ivajlo Yanakiev
Respected Contributor

Re: Cron Question

Nguyen Anh Tien is realy right :)


Pete Randall
Outstanding Contributor

Re: Cron Question

How do you figure that's right? It looks to me like that would run every Friday.


Pete

Pete
Doug Kratky
Frequent Advisor

Re: Cron Question


If you could live with the 4th Friday every month instead of the last Friday, you could do:
0 06 21-28 * 5 /scripts_dir/backup


A. Clay Stephenson
Acclaimed Contributor

Re: Cron Question

This suggested solution
0 06 21-28 * 5 /scripts_dir/backup
will certainly run on the last Friday of the month but sadly it will also run on every other Friday of the month as well. In addition, it will also execute the command every doy of the month from the 21st to the 28th. When weekdays and monthdays are specified in cron, the result is not a logical AND but is rather a logical OR.

Use the solution I suggested earlier.
If it ain't broke, I can fix that.
Prashant Zanwar_4
Respected Contributor

Re: Cron Question

You can do some tricky calculation, like just take last date of month as 30 & 31, you can do it monthwise...
DO a minus on this day and see what is the day..using some date format, if the day turns out to be friday, you can execute the command..

date would be 31,30,28

do 31 - 5,4,3,2,1,0...see what it returns..execute ur script if it is friday...
you can divide the script in 3parts probably..and call from a small script in cron..

Hope it helps
Thanks and regards
Prashant
"Intellect distinguishes between the possible and the impossible; reason distinguishes between the sensible and the senseless. Even the possible can be senseless."
Sergejs Svitnevs
Honored Contributor

Re: Cron Question

My solution:

Crontab (starts custom script every friday) :
0 6 * * 5 /scripts/backup_start

Script (checks condition: if today is last friday then execute main script):

#!/usr/bin/ksh
Last_day_in_month=`cal | sed '/^$/d' | tail -1 | head -1 | awk '{print $NF}'`
Today=`date +%d`
if [ `expr $Today + 7` \=> `expr $Last_day_in_month` ]
then

fi

Regards,
Sergejs
Marlou Everson
Trusted Contributor

Re: Cron Question

Another variation would require adding a few lines to the backup script and 3 cron entries.

# Check if day is Fri
today=$(date +%a)
if [ "$today" != "Fri" ]
then
echo "Okay, will not run now since today is $today and not Fri."
exit
fi


00 06 25-31 1,3,5,7,8,10,12 * /scripts/backup
00 06 24-30 4,6,9,11 * /scripts/backup
00 06 22-28 2 * /scripts/backup

This would still require a method to deal with February when it has 29 days with the 29th on a Friday.

Switching to the first Friday of the month let's you just run the script on days 1 through 7.

Marlou
Anthony Villanueva
Frequent Advisor

Re: Cron Question

Hi,

I think Clay's solution is the simplest and you can be sure that it will run on the last Friday of the month. Why complicate it, right?
f. halili
Trusted Contributor

Re: Cron Question

thanks for all the inputs.
derekh