Created
June 12, 2012 20:14
-
-
Save rcalsaverini/2919867 to your computer and use it in GitHub Desktop.
Get running time of a process given a pid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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