Saturday, March 24, 2012

Check elapsed time

check elapsed time

Here I collected several ways to check elapsed time of agiven PID, when you didn’t have time or date added to the command line

1)            ps –oetimePID

2)            ps –p PID–o etime=

3)            check thetime when the output file is updated

This could be used to check the elapsed time even if the jobis done, as long as you have a start time

4)            ps -eopid, etime, args

5)            A shellscript with a bit of perl (should work on all variants of UNIX)


#!/bin/ksh

#puptime.ksh - Gavin Satur - http://cybergav.in/2009/11/09/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 inputargument. SYNTAX: ksh puptime.ksh <pid>\n"

   exit 999

fi

if [ ! -d /proc/$proc_pid ]; then

   print "\nERROR : No directory forPID $proc_pidin /proc. Check if process is running!\n"

   exit 999

fi

#

#Calculate start time of process and current time in epoch time using a bit ofperl

#

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-friendlyoutput

#

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"





Here we recommend 1) and 4) . It prints out the elapsed time(the “e” in etime) in POSIX format. The output format is like this:

5059 7-13:09:53 /bin/zsh /usr/bin/grml-x fluxbox

…which of course means: PID 5059, elapsed time 7 days, 13hours,09 minutes, 53 seconds.

This is the time I started this cute little window managerfrom the shell.

The ps parameters used are :

-e lists information about evey process running

-o prints information according to the format specified

…so saying “I want the pid, the elapsed time, and all therest of the info” gives you the result up above.

There is also an option stime for -o , but this only returnsthe starting day.

techero@barrioverde ~ % ps -eo pid,stime,args|grep 5059

5059 Feb07 /bin/zsh /usr/bin/grml-x fluxbox

No comments:

Post a Comment