summaryrefslogtreecommitdiff
path: root/Code/killprocess.py
blob: d2237aa5c292e3afcf62a81ecf0a001a288f6560 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import os, signal
def kill_process(process_name):
    try:
        for line in os.popen('ps ax | grep ' + process_name + ' | grep -v grep'):
            fields = line.split()
            if fields[4] == process_name:
                pid = fields[0]
                os.kill(int(pid), signal.SIGKILL)
        return True
    except Exception as e:
        print(f'{e}')
        return False

kill_process('sleep')