Skip to content

Instantly share code, notes, and snippets.

@rcalsaverini
Created June 12, 2012 20:14
Show Gist options
  • Save rcalsaverini/2919867 to your computer and use it in GitHub Desktop.
Save rcalsaverini/2919867 to your computer and use it in GitHub Desktop.
Get running time of a process given a pid
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from subprocess import check_output
from sys import argv
from time import time
cmd = lambda pid: "stat -t /proc/%s | awk '{print $14}'" % (pid,)
def get_start_time(pid):
out = check_output(cmd(pid), shell=True).strip("\n")
return int(out)
def timetuple(deltatime):
hrs = deltatime // 3600
mnt = (deltatime - hrs * 3600) // 60
sec = (deltatime - hrs * 3600 - mnt * 60)
return (hrs, mnt, sec)
if __name__ == "__main__":
pid = argv[1]
tt = (0, 0, 0)
while True:
try:
start_time = get_start_time(pid)
deltatime = int(time() - start_time)
tt = timetuple(deltatime)
if tt[2] % 10 == 0:
print "running for: %sh%2sm%2ss" % tt
except:
print "THE END!\n The process runned for: %2sh%2sm%2ss" % tt
break
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from subprocess import check_output
from sys import argv
from time import time
cmd = lambda pid: "stat -t /proc/%s | awk '{print $14}'" % (pid,)
if __name__ == "__main__":
pid = argv[1]
start_time = int(check_output(cmd(pid), shell=True).strip("\n"))
deltatime = int(time() - start_time)
hrs = deltatime // 3600
mnt = (deltatime - hrs * 3600) // 60
seg = (deltatime - hrs * 3600 - mnt * 60)
print "%2sh%2sm%2ss" % (hrs, mnt, seg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment