Search This Blog

2014-04-21

Start time of a Linux PID

date --date="`ps -p -o lstart= `" +%s

Example where 683 is the PID:

# Only print the start time in ctime format so we can convert it using the date command
ps -p 683 -o lstart=
Wed Apr 16 18:46:32 2014
# pipe the result into the date command to convert to epoch time
date --date="`ps -p 683 -o lstart= `" +%s
1397688392
# Convert from epoch time back to regular time to confirm we’ve done it right
date -d @1397688392
Wed Apr 16 18:46:32 EDT 2014

Alternatively you could do some realllyyy crazy stuff in /proc//stat but I would highly recommend not :P

PID=683
STARTTIME=$(awk '{print int($22 / 100)}' /proc/$PID/stat)
UPTIME=$(awk '{print int($1)}' /proc/uptime)
NOW=$(date +%s)
DIFF=$((NOW - (UPTIME - STARTTIME)))
date -d @$DIFF

Crazy stuff with /proc explanation:
http://stackoverflow.com/questions/16726779/total-cpu-usage-of-an-application-from-proc-pid-stat
http://unix.stackexchange.com/questions/62154/when-was-a-process-started

No comments:

Post a Comment