1752489 Members
5584 Online
108788 Solutions
New Discussion юеВ

Script to run every hr

 
SOLVED
Go to solution
Allanm
Super Advisor

Script to run every hr

Hi!

I am having problems with the attached script when it runs at midnight (next day)...otherwise it runs fine.

Can you suggest what change I can make to run it every 5 mins past the hr successfully.

I don't want to use if statement though and I am running this thru cron.

Thanks,
Allan.
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor

Re: Script to run every hr

Hi Allan:

Am I missing something? Why not 'cron' your script like this (?):

5 * * * * /path_to_script

Regards!

...JRF...
Allanm
Super Advisor

Re: Script to run every hr

Thats what I did but it broke when it ran at 00:05.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Script to run every hr

Hi (again) Allan:

Instead of fiddling around with time computations over midnight, use this Perl snippet:

# perl -MPOSIX -le 'print strftime "%Y%m%d %H",localtime(time()-3600)'
20090224 15

You can use the same 'date' formatting directives you are used to using. This code uses 3600 seconds or ONE hour ago.

Regards!

...JRF...
Patrick Wallek
Honored Contributor

Re: Script to run every hr

>>Thats what I did but it broke when it ran at 00:05.

Broke how?

>>I am having problems with the attached

What kind of problems?

What does it do or not do when it runs after midnight?
Patrick Wallek
Honored Contributor

Re: Script to run every hr

PRESENTHR=`date '+%H'`
LASTHR=`expr $PRESENTHR - 1`

The problem is the above expressions.

When you run this during the midnight hour, which is the '0' hour, and you subtract 1 from, you get '-1'.

You need some logic to determine that '-1' should actually be hour 23 from yesterday. (I think JRF covers this in his response above.)
Allanm
Super Advisor

Re: Script to run every hr

Thanks so much JRF/Patrick.

Just for my knowledge how would I handle this through "date" command.

Thanks
Allan
James R. Ferguson
Acclaimed Contributor

Re: Script to run every hr

Hi (again) Allan:

> Just for my knowledge how would I handle this through "date" command

You would have to subtract your one-hour from the hour field and if that is negative, back off by one day; maybe to the previous month; and perhaps to the previous year...

Hence, why reinvent the wheel...

One helpful piece if you insist on taking this route, might be to install the GNU 'date' command which can do :

# date -d yesterday

Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: Script to run every hr

Shalom,

A Clay's Date Hammer can be useful in these type of situations:

http://mirrors.develooper.com/hpux/caljd-2.25.sh

http://mirrors.develooper.com/hpux/caljd-2.2.pl

I ported it to Linux (A 1 line shell change), if you need.

Regards,

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