LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 02-26-2007, 01:47 AM   #1
kinetik
Member
 
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:

Quote:
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!


ADDED:

OK, think I got it now.

I created a time2sec.awk file containing the following (got coding from experts-exchange.com):

Quote:
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:

Quote:
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
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,947
Blog Entries: 54

Rep: Reputation: 2732Reputation: 2732Reputation: 2732Reputation: 2732Reputation: 2732Reputation: 2732Reputation: 2732Reputation: 2732Reputation: 2732Reputation: 2732Reputation: 2732
Maybe something like
Code:
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
 
  


Reply


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/Novell 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


All times are GMT -5. The time now is 10:09 AM.

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