-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatcher.py
117 lines (101 loc) · 3.3 KB
/
watcher.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os
import sys
import glob
import time
import subprocess
from subprocess import check_output
from datetime import datetime, timedelta
#log_file = '/mnt/hinoki/share/covid-19-logs/errorlog_song'
log_file = '/mnt/hinoki/share/covid19/run/trans_log_song/watcher_log.txt'
import smtplib
from email.mime.text import MIMEText
from email.utils import formatdate
TO_ADDRESS ="[email protected]"
FROM_ADDRESS = "[email protected]"
MY_PASSWORD = "covid19test"
SUBJECT = 'Covid19_error_message'
def create_message(from_addr, to_addr, subject, body):
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = to_addr
msg['Date'] = formatdate()
return msg
def send(from_addr, to_addrs, msg):
smtpobj = smtplib.SMTP('smtp.gmail.com', 587)
smtpobj.ehlo()
smtpobj.starttls()
smtpobj.ehlo()
smtpobj.login(FROM_ADDRESS, MY_PASSWORD)
smtpobj.sendmail(from_addr, to_addrs, msg.as_string())
smtpobj.close()
def my_get_time():
dt = datetime.now()
year, month, day, hour, minute = dt.year, dt.month, dt.day, dt.hour, dt.minute
return year, month, day, hour, minute
def get_pid(name):
try:
sres = check_output(["pgrep","-f",name]).split()
except:
return []
res = [int(i) for i in sres]
return res
def end_program():
pids = get_pid("extract_text.py") + get_pid("translate.py") + get_pid("extract_tweet.py") + get_pid("twitter_translate.py") + get_pid("launch.py") + get_pid("sentiment_classifier.py")
print (pids)
for pid in pids:
os.system("kill -9 {}".format(pid))
def start_program():
print(1)
subprocess.Popen('bash twitter_translate.sh', shell=True)
print(2)
subprocess.Popen('bash translate.sh', shell=True)
print(3)
subprocess.Popen('bash extract_text.sh', shell=True)
print(4)
subprocess.Popen('bash extract_tweet.sh', shell=True)
print(5)
subprocess.Popen('bash sentiment.sh', shell=True)
print(6)
subprocess.Popen('bash topic_classifier.sh', shell=True)
def restart():
end_program()
time.sleep(3)
start_program()
def get_status_line():
line = ''
if len(get_pid("extract_text.py"))==0:
line += "Article extract error\n"
if len(get_pid("translate.py"))==0:
line += "Article translate error\n"
if len(get_pid("extract_tweet.py"))==0:
line += "Tweet extract error\n"
if len(get_pid("twitter_translate.py"))==0:
line += "Tweet translate error\n"
if len(get_pid("launch.py"))==0:
line += "Topic classification error\n"
if len(get_pid("sentiment_classifier.py"))==0:
line += "Sentiment classification error\n"
if (line == ''):
line = 'No error\n'
else:
msg = create_message(FROM_ADDRESS, TO_ADDRESS, SUBJECT, line)
#send(FROM_ADDRESS, TO_ADDRESS, msg)
year, month, day, hour, minute=my_get_time()
date_info = "Date: {}/{} {}:{}\n".format(month, day, hour, minute)
line = date_info + line
return line
def write_status_to_log():
status_line = get_status_line()
with open(log_file, "a+") as f:
f.write(status_line.strip()+'\n')
#end_program()
#exit()
watch_freq = 8
sleep_period = 7200
while (1):
restart()
for i in range(watch_freq):
time.sleep(10)
write_status_to_log()
time.sleep(sleep_period)