Skip to content

Commit 7ccfe40

Browse files
committed
get start time of a process
1 parent 5e6badb commit 7ccfe40

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

py2018/proc_start_time.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
get the start time of a process
3+
e.g. python proc_start_time.py -p 12345
4+
'''
5+
import argparse
6+
import psutil
7+
import os
8+
import time
9+
10+
11+
def get_start_time(pid):
12+
''' return the start time in human readable string '''
13+
p = psutil.Process(int(pid))
14+
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(p.create_time()))
15+
16+
17+
if __name__ == '__main__':
18+
parser = argparse.ArgumentParser()
19+
parser.add_argument("-p", "--pid", help="PID of a process")
20+
args = parser.parse_args()
21+
if not args.pid:
22+
args.pid = os.getpid()
23+
print get_start_time(args.pid)

0 commit comments

Comments
 (0)