#!/bin/ksh # puptime.ksh - Gavin Satur - http://it.cybergav.in/puptime # Simple script to calculate the uptime (elapsed wall clock time) of a process ############################################################################## # # Accept PID and do some basic validation # proc_pid=$1 if [ -z "$proc_pid" ]; then print "\nERROR : Missing input argument. SYNTAX: ksh puptime.ksh \n" exit 999 fi if [ ! -d /proc/$proc_pid ]; then print "\nERROR : No directory for PID $proc_pid in /proc. Check if process is running!\n" exit 999 fi # # Calculate start time of process and current time in epoch time using a bit of perl # proc_stime=`perl -e 'use File::stat; my $filename = "$ARGV[0]"; $sb = stat($filename); printf "%s", $sb->mtime;' /proc/$proc_pid` currtime=`perl -e 'print time;'` # # Calculate process uptime in seconds and then slice'n'dice for human-friendly output # proc_time=$(( currtime - proc_stime )) proc_time_days=$(( proc_time / 86400 )) proc_time_secs=$(( proc_time % 86400 )) proc_time_hours=$(( proc_time_secs / 3600 )) proc_time_secs=$(( proc_time_secs % 3600 )) proc_time_minutes=$((proc_time_secs / 60 )) proc_time_secs=$(( proc_time_secs % 60 )) print "\nUPTIME FOR PROCESS WITH PID $proc_pid = $proc_time_days day(s) $proc_time_hours hour(s) $proc_time_minutes minute(s) $proc_time_secs second(s) \n"