nalocay
nalocay

Reputation: 33

How to get time in seconds since process started, without using etimes?

Trying to use ps to find the number of seconds since a process started. The version of ps I have doesn't support etimes and not sure how else to get this info?

Upvotes: 0

Views: 824

Answers (1)

For process of pid 1234, you could use the mtime field of meta-data of pseudo-file /proc/1234/status. Read proc(5) for more. See also stat(2) and stat(1) and date(1).

So date +%s is giving the current date since the Unix epoch, e.g. 1479125355 now in November 14th, 2016. stat -c %Y /proc/1234/status is giving the start time of process 1234 since Unix epoch. You want the difference. Perhaps use (barely tested, my interactive shell is zsh)
$[$(date +%s) - $(stat -c %Y /proc/1234/status)]; adapt that to your shell.

For example:

 bash -c 'sleep 4; echo $(($(date +%s) - $(stat -c %Y /proc/$$/status)))'

is giving me 4 as expected. Of course the $$ is expanded to the pid of the bash -c command

Upvotes: 3

Related Questions