Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-26-2007, 01:47 AM   #1
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Rep: Reputation: 15
Trying to get ps to display time a process has been running in seconds

Hi again all

I'm using the following command:

ps -eo pid,etime,args --sort start_time | awk {'print $2'}
I'll use this with a grep to get the time a specific process has been running, only problem now is that I want to display the value in seconds. With the above command, I'm getting values like 00:45 or 02:36 etc.

I'd like this to display as in the case of 02:36 as 156 (in seconds), any idea how I can get ps to do this or how I can convert this with a different command/ bc function?

Thanks buds!


OK, think I got it now.

I created a time2sec.awk file containing the following (got coding from

BEGIN { sum = 0; FS = "[^0-9]+"; }
/[0-9]+:[0-9]+:[0-9]+/ { sum += (($1 * 3600) + ($2 * 60) + $3); next;}
/[0-9]+:[0-9]+[^0-9:\.]*[mM]/ { sum += (($1 * 3600) + ($2 * 60)); next;}
/[0-9]+:[0-9]+/ { sum += (($1 * 60) + $2); next;}
/[0-9 ]+[hH][^0-9]*[0-9 ]+[mM][^0-9]*[0-9]+[0-9 \.]*[sS]/ { sum += (($1 * 3600) + ($2 * 60) + $3); next;}
/[0-9 ]+[mM][^0-9]*[0-9]+[0-9 \.]*[sS]/ { sum += (($1 * 60) + $2); next;}
/[0-9]+[0-9 \.]*[sS]/ { sum += $1; };
END { print sum }

To test this, run top in a seperate terminal, then do the following in yet another terminal session:

ps -eo pid,etime,args --sort start_time | grep top$ | awk {'print $2'} | awk -f /data01/home/topaz/scripts/time2sec.awk
This should give you a time in seconds.

Still polishing my script, but so far so good

Last edited by kinetik; 02-26-2007 at 03:12 AM.
Old 02-26-2007, 07:21 PM   #2
Registered: May 2001
Posts: 29,370
Blog Entries: 55

Rep: Reputation: 3555Reputation: 3555Reputation: 3555Reputation: 3555Reputation: 3555Reputation: 3555Reputation: 3555Reputation: 3555Reputation: 3555Reputation: 3555Reputation: 3555
Maybe something like
time2sec() { while read i; do t=$i; i=(${i//:/ }); case "${#i[@]}" in
 3) echo "(${i[0]}*60*60)+(${i[1]}*60)+${i[2]:=0}"|bc -l;;
 2) echo "(${i[0]}*60)+${i[1]}"|bc -l;; esac; done; }
\ps -eo etime --no-headers | time2sec


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to display "real time" view of process running in openSUSE 10.1 ramirez.alex SUSE / openSUSE 6 02-27-2007 03:48 AM
wall clock time of a running process ljmace1953 Linux - General 3 06-26-2006 01:14 PM
Running time active process js_valencia Linux - General 6 02-15-2006 10:14 AM
Change the display of a running process? ejkeebler Linux - Newbie 3 06-16-2004 08:43 AM
I want to figure out which display a process is running on drgowans Linux - Software 4 11-10-2003 11:32 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:19 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration