-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcontrol-stop.sh
executable file
·155 lines (133 loc) · 4.4 KB
/
control-stop.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
# Reading Telegram settings and stand name from the veda.properties file
TELEGRAM_BOT_TOKEN=$(grep "tg_notify_token" ./veda.properties | cut -d'=' -f2 | tr -d ' "')
TELEGRAM_CHAT_ID=$(grep "tg_notify_chat_id" ./veda.properties | cut -d'=' -f2 | tr -d ' ')
STAND_NAME=$(grep "name" ./veda.properties | cut -d'=' -f2 | tr -d ' "')
# Configuration variables
WAIT_INTERVAL=10
CHECK_INTERVAL=20
FORCE_STOP_CHECK_INTERVAL=5
target=".pids/"
# Read all PIDs into an array
pids=()
if [ -e $target ]; then
for f in "$target"/*
do
pid=$(cat "$f")
pids+=("$pid")
done
fi
# Function to send messages to Telegram
send_telegram_message() {
local message="Stand: $STAND_NAME. $1"
curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" \
-d chat_id="$TELEGRAM_CHAT_ID" \
-d text="$message" \
-d parse_mode="Markdown"
}
# Function to stop veda processes
stop_veda() {
local pids=("$@")
# Send SIGTERM to all processes
for pid in "${pids[@]}"; do
echo SEND SIGTERM $pid
kill -s SIGTERM $pid
done
# Wait for processes to stop or for the time specified in WAIT_INTERVAL to pass
for ((i=0; i<$WAIT_INTERVAL; i++)); do
all_stopped=true
for pid in "${pids[@]}"; do
if ps -p $pid > /dev/null; then
all_stopped=false
echo wait stop $pid ...
sleep 1
break
fi
done
if $all_stopped; then
break
fi
done
# Send SIGQUIT to any processes that are still running
for pid in "${pids[@]}"; do
if ps -p $pid > /dev/null; then
echo "Process $pid did not terminate after SIGTERM, sending SIGQUIT"
kill -QUIT $pid
fi
done
echo ""
# Check veda processes for the time specified in CHECK_INTERVAL
for ((i=0; i<$CHECK_INTERVAL; i++)); do
veda_processes_running=false
for pid in "${pids[@]}"; do
if ps -p $pid > /dev/null; then
veda_processes_running=true
echo "VEDA process $pid is still running, waiting..."
break
fi
done
if ! $veda_processes_running; then
echo "No VEDA processes detected, exiting loop."
break
fi
sleep 1
done
# If veda processes are still detected after checking, forcibly stop them
veda_processes_running=false
for pid in "${pids[@]}"; do
if ps -p $pid > /dev/null; then
veda_processes_running=true
echo "Forcibly stopping VEDA process $pid using kill -KILL"
kill -KILL $pid
fi
done
# Check veda processes for the time specified in FORCE_STOP_CHECK_INTERVAL after force stop
for ((i=0; i<$FORCE_STOP_CHECK_INTERVAL; i++)); do
veda_processes_running=false
for pid in "${pids[@]}"; do
if ps -p $pid > /dev/null; then
veda_processes_running=true
echo "VEDA process $pid is still running after force stop, waiting..."
break
fi
done
if ! $veda_processes_running; then
echo "No VEDA processes detected after force stop, exiting loop."
break
fi
sleep 1
done
# Check for any remaining processes at the end of the function
for pid in "${pids[@]}"; do
if ps -p $pid > /dev/null; then
return 1 # Returns false if at least one process is still running
fi
done
return 0 # Returns true if all processes were successfully stopped
}
# Stop veda-bootstrap
start-stop-daemon -Kp $PWD/.pids/veda-pid
# Call the stop_veda function with the pids array
if stop_veda "${pids[@]}"; then
echo "All VEDA processes were successfully stopped."
# Remove the .pids directory and its contents only if all processes were successfully stopped
rm -f -r .pids
else
echo "Failed to stop all VEDA processes."
send_telegram_message "Failed to stop all VEDA processes."
fi
if [ -z $1 ] ; then
echo "STOP VEDA MODULES"
else
# If "all" is provided as an argument, stop all VEDA modules
if [ $1 == "all" ] ; then
echo "STOP ALL VEDA MODULES"
start-stop-daemon -Kp $PWD/veda-pid $PWD/veda
killall -9 veda-*
killall -9 veda
#tarantoolctl stop init_tarantool.lua
pkill tarantool
fi
fi
ps aux | grep veda
exit 0