0

I am trying to find the running time of a process on a linux server. I using etime to achieve this. Here's the code i am using

ps -eo etime,args | grep "process"|awk '{print $1}'

The sample output for this

28-08:42:13

I am then doing the following to convert this to seconds and sending myself a mail when the runtime is less that 30 mins(1800 secs)

ps -eo etime,args | grep "process"|awk '{print $1}'|awk -F'[-: ]+' '/:/ {t=$2+60*$1; print t}'

The problem i am seeing is, when converting to seconds, it's taking the $2(secs) and $1(mins) from the running time and sending me a mail even though the process has been up for 28-08:42:13. Is there a better way of doing the conversion of runtime to secs? Using $3 and $4 from the first output doesn't work for cases where the runtime is less than 1 hour. It's picking a garbage value and returning a different value than the actual running time. Please help.

user3164754
  • 205
  • 2
  • 9
  • Why don't you parse the whole time and get a unix timestamp? See http://stackoverflow.com/questions/1842634/parse-date-in-bash – ypnos Dec 10 '14 at 21:56

2 Answers2

2

etime is human readable elapsed time. etimes is machine readable elapsed time (in seconds):

$ ps -o etimes,etime,cmd $$
ELAPSED     ELAPSED CMD
 515615  5-23:13:35 bash
that other guy
  • 116,971
  • 11
  • 170
  • 194
0

I am using this and it works.

ps -eo etime,args | grep "process"|awk '{print $1}'|awk -F'[-: ]+' '/:/ && NF==5 {t=$5+60*($4+60*($3+24*($2+365*$1))); print t} /:/ && NF==4 {t=$4+60*($3+60*($2+24*$1)); print t} /:/ && NF==3 {t=$3+60*($2+60*$1); print t} /:/ && NF==2 {t=$2+60*$1; print t}'
user3164754
  • 205
  • 2
  • 9