-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_killer.py
66 lines (53 loc) · 1.35 KB
/
process_killer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Simple scirpt for killing processes.
usage:
python process_killer.py
"""
import os
import signal
import subprocess
import sys
def get_processes_by_username(username):
"""Print info about all processes belongs to the given user
Args:
username(str): user name
Returns:
None
"""
output = ''
try:
ps = subprocess.Popen(('ps', '-ef'), stdout=subprocess.PIPE)
output = subprocess.check_output(('grep', username), stdin=ps.stdout)
ps.wait()
except subprocess.CalledProcessError as e:
print e.output
return output
def kill_process(pid):
"""kill process by its pid
Args:
pid(int): process id
Returns:
int: 0 for success, 1 for OSError
"""
try:
os.kill(pid, signal.SIGTERM)
return 0
except OSError:
return 1
def main():
while True:
output = ''
print 'Type username to show his processes!'
output = get_processes_by_username(raw_input())
print output
if output == '':
print 'Nothing found!'
break
print 'Type pid to kill process!'
errno = kill_process(int(raw_input()))
if errno == 0:
print 'Process terminated!'
else:
print 'Error! Process cannon be terminated!'
if __name__ == '__main__':
main()