Repository navigation
Expand file tree
/
Copy pathkill_process.py
More file actions
46 lines (42 loc) · 1.48 KB
/
Copy pathkill_process.py
File metadata and controls
46 lines (42 loc) · 1.48 KB
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
import argparse
import subprocess
import sys
FAIL = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
ENDC = '\033[0m'
OKGREEN = '\033[92m'
def display_error(message):
print FAIL + BOLD + UNDERLINE + message + ENDC
def display_result(message):
print OKGREEN + BOLD + message + ENDC
def process_action(func1,func2, mes1, mes2, mes3):
parser = argparse.ArgumentParser(description='This Python script kills process by name')
parser.add_argument('-p', '--process', nargs='+', type=str, required=True, help='The name of the process')
args = parser.parse_args()
if len(sys.argv) < 4:
if args.process:
for i in args.process:
output = subprocess.Popen(['pidof', i], stdout=subprocess.PIPE).communicate()[0]
process_id = [str(i) for i in output.split()]
if process_id:
for i in process_id:
process = subprocess.call(["kill", "-9", i])
func2(mes2)
print args.process
else:
func1(mes2)
sys.exit(1)
func2(mes3)
print args.process
else:
func1(mes1)
parser.print_help()
print "Aborting ..."
def main():
message_error1 = "This python script takes exactly one argument \n "
message_error2 = "There is no process with this name, check the process name"
message_result = "*** killed process ***\n"
process_action(display_error,display_result, message_error1, message_error2, message_result)
if __name__ == "__main__":
main()