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).
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