-
Notifications
You must be signed in to change notification settings - Fork 6
/
nocrash.py
63 lines (46 loc) · 1.6 KB
/
nocrash.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
#!/usr/bin/python3
# coding=utf-8
# This script replaces the original nocrash.sh functionality with a pure Python approach.
import platform
import os
import subprocess as sp
from time import sleep
import logging
import sys
# Set the Python Executable based on this being stored - we refer to this later on for subprocess calls.
PY_EXECUTABLE = sys.executable
# Log to errorlog.txt so that !!/errorlogs shows us restarts
logging.basicConfig(
filename='nocrashlog.txt',
level=logging.INFO,
format='%(asctime)s:%(levelname)s:%(message)s')
count = 0
crashcount = 0
stoprunning = False
ecode = None # Define this to prevent errors
# Make a clean copy of existing environment variables, to pass down to subprocess.
environ = os.environ.copy()
def log(message):
logging.info('[NoCrash] {}'.format(message))
print '[NoCrash] {}'.format(message)
def warn(message):
logging.warn('[NoCrash] {}'.format(message))
print '[NoCrash] {}'.format(message)
def error(message):
logging.error('[NoCrash] {}'.format(message))
print '[NoCrash] {}'.format(message)
while stoprunning is False:
log('Starting')
command = (PY_EXECUTABLE + ' bot.py').split()
try:
ecode = sp.call(command, env=environ)
except KeyboardInterrupt:
# print "[NoCrash] KeyBoard Interrupt received.."
ecode = 6
log('Exited with ecode {}'.format(ecode))
if ecode == 6:
log('Stopping')
stoprunning = True
else:
error('Died for unknown reason -- check logs. Sleeping before restart')
sleep(5)