user3164754
user3164754

Reputation: 215

Running time in secs for a process in linux

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.

Upvotes: 0

Views: 227

Answers (2)

user3164754
user3164754

Reputation: 215

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}'

Upvotes: 0

that other guy
that other guy

Reputation: 123640

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

Upvotes: 2

Related Questions