0

How can I get the start time of a process on macOS? ps -o lstart and ps -o start print formatted dates instead of a Unix timestamp.

Ideally this would be portable across Unix (i.e. no /proc obviously).

taway
  • 1,099
  • 1
  • 7
  • 9

1 Answers1

0

lstart uses the format specified by the locale, and if you use LC_ALL=C, you can get a consistent, simple format:

$ TZ=UTC LC_TIME=C ps -o lstart= -p $$
Thu May 11 01:03:52 2017
$ LC_TIME=C ps -o lstart= -p $$
Thu May 11 10:03:52 2017

Then there are several tools that can be used to convert the formatted date to a timestamp. For portability, perhaps Perl:

$ (export LC_TIME=C TZ=UTC; ps -o lstart= -p $$ | perl -ne 'use Date::Parse; printf "%s\n", str2time($_)')
1494882318
Community
  • 1
  • 1
muru
  • 4,723
  • 1
  • 34
  • 78
  • Does this handle DST issues and anything else I'm not thinking of? It would seem like it would be confused on the "fall back" hour, since there are two possible timestamps for a natural language date in that case. – taway May 18 '17 at 14:29
  • @taway if timezones could be a problem, you could set `TZ` to `UTC` as well – muru May 18 '17 at 16:09