The Robservatory

Robservations on everything…

 

See exactly when an app launched

Update: I guess I should have searched here before I posted this—I wrote up another solution a few years ago, and that one includes a Keyboard Maestro implementation. Whoops! As this one's another method, though, I'll leave it up.

I was working on some stuff for our upcoming Usher 2 release, and needed to know how long Usher had been running. A quick web search found this post, where one of the comments had an answer that works well in macOS:

ps -p pid -o lstart=

Replace pid with the process ID (PID) from ps -ax for the app or process in question, and you're done. But it's possible to make it even easier to use by automating the task of getting the process ID. Here's what I came up with:

ps -p `ps ax | grep [U]sher$ | cut -c 1-5` -o lstart=

The bit between the backticks gets the matching process line from ps, then uses cut to keep just the first five columns, which contain the PID.1When the shell encounters backticks, it processes the commands within those backticks before processing the rest of the command—in this case, the backticked command returns the PID.

There are a few things there that warrant a bit of explanation:

  • The square brackets around the U in Usher get rid of a second output line from grep, as explained here.
  • The $ at the end of Usher tells grep to only match that word at the end of the line. This isn't important for Usher, but for an app like Safari or Chrome—which spawns multiple processes—it insures that grep only returns a match for the actual app itself:
    /Applications/Safari.app/Contents/MacOS/Safari
  • The equal sign after lstart indicates to ps that you want a custom header, which would normally be text after the equal sign. By leaving that text blank, the header is skipped, so the output is on just one line.
  • There's no error checking of any sort, so if the app or process doesn't exist, this will return all sorts of error messages.

When I ran the above command, I saw exactly what I was hoping to see:

$ ps -p `ps ax | grep [U]sher$ | cut -c 1-5` -o lstart=
Sun Apr 12 07:26:36 2020

Replace [U]sher with the name of a running app, and it should work the same way for you.

1 Comment

Add a Comment
  1. Thank you, I learned something new! With apple Mail only this works for me:

    ps -p $(pgrep [M]ail$) -o lstart=

    maybe because of some more processes with mail like: 218: 491 ?? S 0:02.55 /Users/xx/Library/Application Support/MailHub/MailHubUserAgent.app/Contents/MacOS/MailHubUserAgent --command /Users/xx/Library/Containers/com.apple.mail/Data/Library/Mail/MailHub/.MailHubUserAgent

Leave a Reply

Your email address will not be published. Required fields are marked *

The Robservatory © 2020 • Privacy Policy Built from the Frontier theme