-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrespawn.py
52 lines (42 loc) · 1.75 KB
/
respawn.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
import os
from signal import SIGKILL, SIGTERM
import psutil
from datetime import datetime
import pywikibot
from pywikibot import config
proceses = {
'ircartcounter-pl': 'masti/artnos pl &',
'ircartcounter-szl': 'masti/artnos szl &',
'ircartcounter-csb': 'masti/artnos csb &',
'ircartcounter-tr': 'masti/artnos tr &',
}
def main(*args):
local_args = pywikibot.handle_args(args)
force = False
for arg in local_args:
if arg == '-force':
force = True
# enable logging
with open("masti/pid/respawn.log", "a") as logfile:
# run for all processes
for pname in proceses.keys():
# get pidfile name
pidname = f'masti/pid/{pname}.pid'
try:
file = open(pidname, "r")
pid = int(file.readline())
file.close()
except (FileNotFoundError, ValueError) as e:
logfile.write(f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} {pname} (File:{pidname} NOT FOUND\n')
pid = None
if force:
print(f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} {pname} (PID:{pid}) killing...respawning...')
logfile.write(f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} {pname} (PID:{pid}) killing...respawning...\n')
os.system(f'kill -9 {pid}')
os.system(f'{proceses[pname]}')
elif pid not in psutil.pids():
print(f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} {pname} (PID:{pid}) is dead... respawning...')
logfile.write(f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} {pname} (PID:{pid}) is dead... respawning...\n')
os.system(f'{proceses[pname]}')
if __name__ == '__main__':
main()