Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

See how long a given process has been running UNIX
I wanted to find out how long a certain background process had been running. There's a column for CPU Time in Activity Monitor, but that's not real clock time.

It turns out you can get this information with ps, via the etime keyword. So to get a list of every running process, in decreasing order of run time, just use this command:
ps -ax -o etime,command -c
To see the results for a single process, just add a grep at the end for the process' name. For example:
$ ps -ax -o etime,command -c | grep AppleVNCServer
03-08:09:16 AppleVNCServer
So on my Mac, the AppleVNCServer has been running for three days, eight hours, nine minutes, and 16 seconds. I have a need to do this pretty regularly, so I turned it into a simple command line app:
#!/bin/bash

# Display the time a given process has been running
# Use the process name when calling the command

ps -ax -o etime,command -c | grep $1
I saved that to a file named psup, and made it executable with chmod 755 psup. Now I can just type psup SomeProcess to see the uptime for SomeProcess.
    •    
  • Currently 2.80 / 5
  You rated: 1 / 5 (10 votes cast)
 
[8,466 views]  

See how long a given process has been running | 9 comments | Create New Account
Click here to return to the 'See how long a given process has been running' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
See how long a given process has been running
Authored by: ehemmete on Nov 28, '12 09:01:51AM
Instead of a separate shell script to do that you could make an alias. If you don't already have one, create ~/.profile (or ~/.bash_profile or ...) and add
 alias psup='ps -ax -o etime,command -c | grep' 
Then open a new terminal window and you can use
psup AppleVNCServer
or whatever. No worries about $PATH this way. Not trying to say a shell script is a bad idea, just another option.

[ Reply to This | # ]
See how long a given process has been running
Authored by: stevebr on Nov 28, '12 10:52:53AM

Very helpful. Thanks. Here's a little variation that will pull the whole list if there's no argument following psup, done as a bash function rather than an individual script file:

### uptime of given process or everything if none given
psup () { if [ $1 ]; 
	then
		ps -ax -o etime,command -c | grep $1 ;
	else
		ps -ax -o etime,command -c | less ;
	fi
}

I'm sure there's a far more efficient way of writing that.



[ Reply to This | # ]
See how long a given process has been running
Authored by: maevis on Nov 28, '12 05:20:13PM
Slightly more succinct:

psup() { ps acxo etime,command | grep -- "$1"; }

Now when no search pattern is supplied, grep will just match against an empty string and output the entire list instead of complaining about missing arguments.

The double dash ensures that search patterns beginning with a dash don't get mistaken for grep flags. This seems a good idea because login shells are prefixed with a dash, and this way commands like psup -bash will work as intended.



[ Reply to This | # ]
See how long a given process has been running
Authored by: stevebr on Nov 29, '12 11:29:48AM

Thanks. This is exactly what I was going for in my crude version above.



[ Reply to This | # ]
See how long a given process has been running
Authored by: sault on Nov 29, '12 05:48:11AM

Nice hint. Thanks.



[ Reply to This | # ]
See how long a given process has been running
Authored by: llee on Nov 29, '12 12:08:04PM

This is a great hint (but that shouldn't come as a surprise to too many site devotees, considering who submitted it).



[ Reply to This | # ]
See how long a given process has been running
Authored by: vsopal on Nov 30, '12 08:06:04AM

Very nice evolution of script.
But is there a way to tell how long application was active (i.e. in front)?
I meant something for tracking time of application usage by user.

Edited on Nov 30, '12 08:06:56AM by vsopal



[ Reply to This | # ]
See how long a given process has been running
Authored by: xhinter on Dec 14, '12 05:10:09AM

Maybe HPULoad http://fernlightning.com/doku.php?id=software%3Ahpuload%3Astart



[ Reply to This | # ]
See how long a given process has been running
Authored by: xhinter on Dec 14, '12 05:19:19AM
alias psup='_() { ps acxo etime,command | grep -i -- "$1"; }; _'

[ Reply to This | # ]