For a recent customer support question, I needed to know how long our app Witch had been running. There are probably many ways to find this out, but I couldn’t think of one. A quick web search found the solution, via ps and the etime flag.
You need the process ID (pid), which you can find via ps ax | grep [a]ppname.1That [s]quare brackets around the first letter are there so grep won’t find itself—and thus list itself in the output. In my case, Witch runs a background task called witchdaemon, so I did it this way…
$ ps -ax | grep [w]itchd 774 ?? 26:40.73 /Users/robg/Library/PreferencePanes...[trimmed]
With the pid, the command to find that process’ uptime is:
$ ps -o etime= -p "774" 11-03:17:12
The elapsed time readout is in the form of dd-hh:mm:ss, so Witch had been running for 11 days and a few hours and minutes. Note that you can combine these steps, getting the process ID and using it in the ps command all at once:
ps -o etime= -p "`ps -ax | grep [a]ppname | cut -d ' ' -f 1`"
It’s messy looking, but this form saves time and typing.
June 2018 Addendum: If you add the lstart flag, you can see the exact start date and time for the process. For example:
$ ps -o lstart= -o etime= -p "16866" Tue Jun 5 05:49:19 2018 09-06:11:25