LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   monitoring the time in a process with shell (https://www.linuxquestions.org/questions/programming-9/monitoring-the-time-in-a-process-with-shell-4175506745/)

Jykke 06-02-2014 05:35 AM

monitoring the time in a process with shell
 
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?

evo2 06-02-2014 05:40 AM

Hi,

perhaps I'm missing something here, but is there some reason you can't use
Code:

date +%s
?

Evo2.

Jykke 06-02-2014 06:02 AM

No, that was the one I missed myself ;) Thx


All times are GMT -5. The time now is 08:54 PM.