how to determine the runtime and start time of a Linux process

Yesterday, I needed to determine the runtime of a Linux process for a monitoring script.

Cos the format for start_time of the ps command may change if the process was not started in the same year, I decided to take the neccessary informations from the /proc/<PID>/stat file.

In this file the process start time since boot is defined at the twenty-second field, expressed in Jiffies – the scale unit of the system timer. (One Jiffie is one tick of the system timer).

To convert Jiffies to seconds I just have to divide the number of Jiffies by the frequency (hertz) of the system timer, which is defined in the Linux Kernel header file include/asm-generic/param.h. The frequency may differ between Linux kernel versions and hardware platform! On my Linux systems the frequency is 100 HZ.

In a shell script the following line will determine the start-time of a process since boot time.

HZ="100"
((p_seconds_since_boot = $(cat /proc/$PID/stat | cut -d " " -f 22) / $HZ ))

To get the absolute start time I am adding the boot time of the Linux system from the /proc/stat file to the start time of
the process.

boottime=$(grep btime /proc/stat | cut -d " " -f 2)
(( p_starttime = $boottime + $p_seconds_since_boot ))

To get the runtime in seconds of the process I just subtract the absolute start time of the process from the actual time.

now=$(date "+%s")
(( p_runtime = $now - $p_starttime ))

Download the nagios / icinga plugin from my projects page or the check the following example which shows a simple shell script that takes a process string and determine the runtime and the start time.

#!/bin/bash
#------------------------------------------------------------
# starttime.sh
#------------------------------------------------------------


if (($# != 2)); then
        echo -e "\n\tUsage: $0 -p \"process string\" \n" >&2
        exit 4
fi

while getopts ":p:" opt; do
  case $opt in
    p)
                process="$OPTARG"
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
     :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

PID=$(pgrep -oxf "$process")
test -z $PID && {
        echo -e "\n\tError: \"$process\" not found\n\n"
        exit 4
}

test -f /proc/stat || {
        echo -e "\n\tError: \"/proc/stat\" not found\n\n"
        exit 4
}

HZ="100" 
now=$(date "+%s")

boottime=$(grep btime /proc/stat | cut -d " " -f 2)

((p_seconds_since_boot = $(cat /proc/$PID/stat | cut -d " " -f 22) / $HZ ))

(( p_starttime = $boottime + $p_seconds_since_boot ))

(( p_runtime = $now - $p_starttime ))

echo -en "\nProcess \"$process\" is running since "
echo -e "$p_runtime Seconds (Started: $(date -d @$p_starttime))\n"
#------------------------------------------------------------
# eof
#------------------------------------------------------------

Output of the script for the pidgin process.

roger@slayer:~/scripts/starttime$ ./starttime.sh -p pidgin

Process "pidgin" is running since 13795 Seconds (Started: Sa 31. Mär 14:28:13 CEST 2012)

Related Posts: