I have a shell script that is doing stuff, submitting other processes mainly.
Now I have a timer withing a while loop that monitors if the stuff has been running too long it will exit.
The problem is that I am running the program overnight and I now had a problem due to change of month.
I post few snippets from the script:
Code:
#!/bin/sh
gettime () {
years=`date +"%y"`
months=`date +"%m"`
days=`date +"%d"`
hours=`date +"%H"`
mins=`date +"%M"`
secs=`date +"%S"`
gettime=`echo $years*31104000+$months*2592000+$days*86400+$hours*3600+$mins*60+$secs | bc`
echo $gettime
}
starttime=`gettime`
submit_run_process.sh
actualtime=`gettime`
time_diff=`echo $actualtime - $starttime | bc`
echo "Runtime: " $time_diff
if [ $time_diff -lt 7200 ] ; then
echo "Runtime less than 2 hours from submit, suspicious - exit"
exit
fi
The problem is that my script assumes that month is 30 days and the multiplicator in seconds is 30*24*3600=2592000, now in May we had 31 days so the script had suddenly negative runtimes between May 31 and June 1 around midnight (which is then less than required 7200 seconds), ok I could take a multiplicator 31*24*3600 which would pass the if sentence, but in months with less than 31 days I would always have more than the required 2 hrs even if it was not so.
I am not keen on putting the whole calender into my subroutine and I do not want to bind another language into this, I would rather stick to shell, but at the moment I am a bit short of ideas. Any ideas? I recall there was some command that counts seconds from start? use uptime or so instead of date?