-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbootup.py
More file actions
69 lines (47 loc) · 1.72 KB
/
bootup.py
File metadata and controls
69 lines (47 loc) · 1.72 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
import os
import sys
import subprocess
import logging
from logging.handlers import RotatingFileHandler
import time
logger = logging.getLogger("bootup")
level = logging.getLevelName(logging.INFO)
logger.setLevel(level)
#self.logger.setLevel(logging.INFO)
fh = RotatingFileHandler("bootup.log",
maxBytes=1000000, # 1Mb I think
backupCount=5)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info("bootup.py has started")
try:
args = sys.argv[1:][0]
except:
args = 0
logger.info("bootup passed args %s" % (args))
process_name = "main.py"
p = subprocess.Popen(['ps', '-Af'], stdout=subprocess.PIPE)
out, err = p.communicate()
logger.debug("pid search results %s" % (out))
tmp = os.popen("ps -Af").read()
if process_name not in tmp[:]:
logger.info("The process is not running. Lets Restart")
"""Use nohop to run as daemon"""
newprocess = "nohup sudo python %s &" %(process_name)
os.system(newprocess)
elif args == '-r':
logger.info("Forced restart requested")
for line in out.splitlines():
if process_name in line:
logger.info("Process found %s so killing" % (line))
pid = int(line.split(None, 2)[1])
#os.kill(int(pid), 0)
os.system('sudo kill %d' % (pid))
time.sleep(2)
logger.info("Now restarting the process %s" %(process_name))
newprocess = "nohup sudo python %s &" %(process_name)
os.system(newprocess)
else:
logger.info("Process is already running")