Skip to content

Instantly share code, notes, and snippets.

@mengqingzhong
Last active August 29, 2015 14:18
Show Gist options
  • Save mengqingzhong/9009c0b74005ee088ebe to your computer and use it in GitHub Desktop.
Save mengqingzhong/9009c0b74005ee088ebe to your computer and use it in GitHub Desktop.
#!/bin/bash
function show_elapsed_time()
{
user_hz=$(getconf CLK_TCK) #mostly it's 100 on x86/x86_64
pid=$1
jiffies=$(cat /proc/$pid/stat | cut -d" " -f22)
sys_uptime=$(cat /proc/uptime | cut -d" " -f1)
last_time=$(( ${sys_uptime%.*} - $jiffies/$user_hz ))
lastday=$(( $last_time/3600/24 ))
lasthour=$(( ($last_time-$lastday*3600*24)/3600 ))
lastminute=$(( ($last_time-$lastday*3600*24 - $lasthour*3600)/60 ))
lastsecond=$(( ($last_time - $lastday*3600*24 - $lasthour*3600 - $lastminute*60) ))
echo "the process $pid lasts for $last_time seconds. That is $lastday days $lasthour hours $lastminute minutes $lastsecond second."
}
if [ $# -ge 1 ];then
for pid in $@
do
show_elapsed_time $pid
done
fi
while read pid
do
show_elapsed_time $pid
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment