-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminator.py
50 lines (43 loc) · 1.51 KB
/
terminator.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
from subprocess import check_output as co, CalledProcessError, call
from os import kill
from signal import SIGTERM
from sys import argv, executable
import sys
TEST_PNAME = 'motion'
def getpid(name = TEST_PNAME):
try:
o = co(['ps','-fC',name]).decode()
ol = o.split('\n')
del ol[0]
for item in ol:
il = item.split(' ')
il_ = []
for prop in il:
if prop != '':
il_.append(prop)
if il_ != []:
user, pid, ppid, c, stime, tty, time = il_[:7]
cmd = ' '.join(il_[7:])
return int(pid), ''
except CalledProcessError as e:
return None, f'{name!r} nicht gefunden'
def handlepg(name = TEST_PNAME, mode = 'term'):
pid, err = getpid(name = name)
if pid != None:
try:
kill(pid, SIGTERM)
return 'Erfolgreich beendet'
except PermissionError:
print('Besitzt nicht die ausreichenden Rechte, Befehl wird mit sudo auto-wiederholt...')
if __name__ == '__main__':
call(['sudo', executable, argv[0], name, 'tryingagain'])
else:
call(['sudo', executable, __name__ +'.py', name, 'tryingagain'])
return 'Erfolgreich beendet'
else:
return f'FEHLER: {err}'
if __name__ == '__main__':
print(f'Terminator v0.1, using processname {argv[1]!r}...')
res = handlepg(argv[1])
if len(argv) < 3:
print(res)