-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.sh
83 lines (73 loc) · 2.06 KB
/
run.sh
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
#!/bin/sh
totalErrors=0
function pushService() {
# Logic of Push Service
logFile=$1
errorLog=$(cat logs/${logFile} | nc termbin.com 9999 | tr -d '\0')
lastLine=$(cat logs/${logFile} | tail -1)
curl -s -H "Priority: 4" -H "Click: ${errorLog}" -d "${lastLine}" ntfy.sh/${NTFY_TOPIC} >/dev/null
}
function continue_handler() {
# Create a lock of suspended process
echo >suspend.lock
}
function cleanup() {
if [ -n "$APP" ]; then
kill "$APP"
fi
if [ -n "$MAIN" ]; then
kill "$MAIN"
fi
}
function logging() {
if [ -n "$LOG_DATE" ]; then
logFile="main_${LOG_DATE}.log"
errors=$(grep -ic "error" "logs/${logFile}")
# When error found in logFile
if [ $errors -gt $totalErrors ]; then
totalErrors=$errors
pushService $logFile
echo "Something went wrong: Check ${PWD}/logs/${logFile} for details"
fi
fi
}
# Catch SIGCONT Signal (Continue Suspend Process)
trap continue_handler SIGCONT
# Creating logs directory (If not exist)
if test ! -d "logs/"; then
mkdir "logs/"
fi
# Starting and logging bot
LOG_DATE=$(date '+%Y-%m-%d_%H-%M-%S')
nohup python app.py > >(tee -a logs/main_${LOG_DATE}.log) 2>&1 </dev/null &
APP=$!
nohup python -u main.py START > >(tee -a logs/main_${LOG_DATE}.log) 2>&1 </dev/null &
MAIN=$!
# Catch SIGINT Terminate Signal and Kill the Python Processes
trap cleanup SIGINT SIGTERM
# On loop
while true; do
sleep 1
# If remote logging is enabled
if [ -n "$NTFY_TOPIC" ]; then
logging
fi
if test -f "main.lock"; then
# Restart request
rm -f main.lock
rm -f terminate.lock
LOG_DATE=$(date '+%Y-%m-%d_%H-%M-%S')
echo "[MAIN] Process main.py has been restarted..." | tee -a logs/main_${LOG_DATE}.log
nohup python -u main.py RESTART > >(tee -a logs/main_${LOG_DATE}.log) 2>&1 </dev/null &
MAIN=$!
elif test -f "terminate.lock" && ! test -f "main.lock"; then
# Terminate request
rm -f terminate.lock
break
else
# Checks If main child process is running (if not then terminate the loop)
if [ $(ps $MAIN | wc -l) != 2 ]; then
break
fi
fi
done