-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmars.py
69 lines (54 loc) · 1.74 KB
/
mars.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
64
65
66
67
68
69
import psutil
import subprocess
import os
import time
import schedule
import daemon
import args
import sys
#pas the arguments
process_name = args.process_name
max_fail = args.max_fail
restart_interval = args.restart_interval
input_command = args.input_command
check_interval =args.check_interval
CUNT = 0
timer = 0
daemon.logger.info("######## user input ######## \n \
\trestart_interval = %s, \n \
\tmax_fail = %s , \n \
\tprocess_name = %s , \n \
\tcheck_interval = %s , \n \
\tinput_command = %s \n ",
restart_interval, max_fail, process_name, check_interval, input_command )
def checkIfProcessRunning(processName):
'''
Check if there is any running process that contains the given name processName.
'''
#Iterate over the all the running process
for proc in psutil.process_iter():
try:
# Check if process name contains the given name string.
if processName.lower() in proc.name().lower():
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False
# Check if any process was running or not. then run it
def startpc():
global CUNT
if checkIfProcessRunning(process_name) is False:
daemon.logger.info(" %s process is not run try to run",process_name)
os.system(input_command)
CUNT = CUNT + 1
if CUNT >= int(max_fail):
daemon.logger.error("max number of retires reached")
exit()
time.sleep(int(restart_interval))
else:
daemon.logger.debug("is running with pid: %s", process_name)
CUNT = 0
schedule.every(int(restart_interval)).seconds.do(startpc)
while 1:
schedule.run_pending()
time.sleep(1)