Last Updated: December 11, 2016
·
5.948K
· calsaverini

Check running time of long processes in Linux

To check how many seconds have elapsed since a process started you can use this small shell script:

#!/bin/bash
init=`stat -t /proc/$1 | awk '{print $14}'`
curr=`date +%s`
seconds=`echo $curr - $init| bc`
name=`cat /proc/$1/cmdline`
echo $name $seconds

save it on a file called sincetime and give permissions for your user to run it and put it in your $path. Than, the command:

sincetime <pid>

will return the name of the process with the given pid and its age in seconds.

Also, you can combine it with grep and ps and have this:

#!/bin/bash
pidlist=`ps ax | grep -i -E $1 | grep -v grep | awk '{print $1}' | grep -v PID | xargs echo`
for pid in $pidlist; do
    sincetime $pid
done

If you put this in a file called "greptime" with x permission, in your path and blablabla, you can run it like this:

greptime <pattern>

to get all processes whose names match <pattern> (a string or regexp) and their ages in seconds.

1 Response
Add your response

Hi,

Just a note to say thanks, and suggest an improvement...

Using a format specifier in the arguments to stat would make things clearer, rather than relying on the mysterious column 14 of the terse output:

stat /proc/$1 --printf=%Z

Mark

over 1 year ago ·