Help with time comparison shell script for HP-UX

I am using Korne Shell in HP-Ux. Can someone give me and idea on how I can write a shellscript on how to do this please:-

On our HP-UX server, a batch file is run every evening at about 6:30pm. The first step of this batch file will touch an empty "flag" file to indicate that the batch has already started running. This flag is removed at the end of the evening batch file. So bascially, as long as this flag exists, the batch file is not allowed to be run by another operator. The problem is I need to built in some testing that if the batch file has already been run once within the 24 hours cycle, it mustn't be run again.

I thought about setting a timestamp into the "flag" file I create at the beginning of the batch run and call it "BatchRunning". Then rename this flag file and call it "batchRan" so it will be served as sort of time marker for comparison later. So if the batch file is re-run then a comparison can be made between the two files "BatchRunning" and "batchRan". If "BatchRunning" is less than "batchRan" by 24 hours then abort the batch file immedately and display a message to the operator.

So that's the theory, how do I script this in Korne Shell please?? :confused:

Please help me ... as I have tried to search the forum for clues .... and I am really stuck now.. :frowning: In my search, on time comparison, it picked up the "datecalc" script but I am not sure if it can be applied to my problem ( it looks far too complicated).

Many thanks in advance,

Why dont you look at crontab ? Using crontab, you can schedule your current script to run once a day at 1830.

No, can't use crontab at all because the batch file is not always run at 6:30pm. Sometimes, the end of days can finish late. On some of our servers, the evening batch file can only be run if another system has finished. This is why the batches can only be kicked off by our operators in a manual mode if you like.

Thanks for your suggestion anyway :slight_smile: ....

  • You can use 'touch' to create the lock files.

  • day=`date +%d` gives the day value of now

  • minutes=`date +%m` gives the minute value of now

  • ls -l /path/to/dir/file | awk '{print $7}' gives the date entry of the file

  • file1 -nt file2
    True, if file1 exists and is newer than file2.

  • file1 -ot file2
    True, if file1 exists and is older than file2.

With this information you should be able to write the script.

Just a little correction.

date +%m

gives the month in digits

date +%M

gives the minute value of now.

Try...

SECS=$(perl -e '@q=stat($ARGV[0]); print time-$q[9]' batchRan)
if [[ $SECS -lt 82800 ]]
then
    echo Less than 23 hours since last run
    exit
fi

Just to say thank you all for looking at my initial query. Using some of the hints given here, I have come up with a script which does the job. Being a newbie to Unix Support, I have found this forum very supportive. :slight_smile: