-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupervisor.sh
More file actions
239 lines (184 loc) · 5.54 KB
/
supervisor.sh
File metadata and controls
239 lines (184 loc) · 5.54 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/bash
# Supervisor script.
# See help section below.
# Configuration file: config-supervisor.conf
_cmd="$1"
_config_file="config-supervisor.conf"
_SV_CTRL_LOG_FILE="log-supervisor-ctrl.log"
_SV_LOG_FILE="log-supervisor.log"
_this_script="${BASH_SOURCE[0]}"
_required_programs="rkill" # List of required programs which are probably not installed by default
# Load config file
_config_file_loaded=false
if [[ -e "$_config_file" ]] ; then
source "$_config_file"
_config_file_loaded=true
else
echo "Error: Configuration file ${_config_file} not found"
fi
_supervisor_name="${sv_name}_supervisor"
# Check if required programs are installed
_all_required_programs_installed=true
for _prog in $_required_programs; do
if ! command -v "$_prog" &> /dev/null ; then
echo "Error: The required program ${_prog} is not installed."
_all_required_programs_installed=false
fi
done
# Show installation help
if [[ $_all_required_programs_installed == false ]] ; then
echo "rkill could be installed on ubuntu with apt-get install pslist"
fi
# Show help
if [[ -z "$_cmd" || "$_cmd" == "-h" || "$_cmd" == "--h" || "$_cmd" == "help" ]] ; then
echo "Supervisor script"
echo "${_this_script} <command>"
echo ""
echo -e "<command>\t\t The command"
echo -e "\t\t\t start Start the supervisor and the supervised program"
echo -e "\t\t\t stop Stop the supervisor and the supervised program"
echo -e "\t\t\t restart Restart the supervisor and the supervised program"
echo -e "\t\t\t status Show the status of the supervisor and the supervised program"
echo -e "\t\t\t config Show configuration"
echo ""
echo ""
echo "Configuration"
echo ""
echo "Configuration file: $_config_file"
echo ""
echo ""
echo "Log files"
echo ""
echo -e "$_SV_LOG_FILE\t Supervisor"
echo -e "$_SV_CTRL_LOG_FILE\t Supervisor control"
echo ""
echo ""
echo "Installation"
echo "Copy the supervisor script ${_this_script} to your project folder or to some other folder."
echo " Each supervisor instance currently needs its own folder. Create a configuration file $_config_file"
echo " in the same folder. For help, see the configuration file template. After configuration, "
echo " the supervisor could be started by ${_this_script} start."
exit
fi
if [[ $_all_required_programs_installed == false ]] ; then
exit 1
fi
if [[ $_config_file_loaded == false ]] ; then
exit 1
fi
log() {
local msg="$1"
local logfile="$2"
if [[ ! -z "$logfile" ]] ; then
echo "$(date -Iseconds) -- ${msg}" >> "$logfile"
else
echo "$(date -Iseconds) -- ${msg}"
fi
}
#################################################################################
# Run the program supervised
#################################################################################
if [[ "$_cmd" == "_run" ]] ; then
_stopped=false
_term() {
_stopped=true
log "Caught SIGTERM signal!" "$_SV_LOG_FILE"
rkill -TERM "$child"
}
_kill() {
_stopped=true
log "Caught SIGKILL signal!" "$_SV_LOG_FILE"
rkill -9 "$child"
}
trap _term SIGTERM
trap _kill SIGKILL
log "Start Supervisor." "$_SV_LOG_FILE"
while [[ "$_stopped" != "true" ]]; do
log "Start Program '$sv_program'" "$_SV_LOG_FILE"
"$sv_program" &
child=$!
wait "$child"
_exit_code=$?
log "Program stopped with exit code ${_exit_code}." "$_SV_LOG_FILE"
[[ "$_stopped" == "true" ]] && break
log "Restart later." "$_SV_LOG_FILE"
sleep $sv_restart_delay
done
log "Stop Supervisor." "$_SV_LOG_FILE"
exit 0
fi
#################################################################################
# Start
#################################################################################
if [[ "$_cmd" == "start" ]] ; then
pgrep -f "$_supervisor_name" && echo "Already running" && exit
log "Start" "$_SV_CTRL_LOG_FILE"
"$_this_script" "_run" "$_supervisor_name" >/dev/null 2>&1 & disown
exit 0
fi
#################################################################################
# Stop
#################################################################################
if [[ "$_cmd" == "stop" ]] ; then
_getPid() {
_pid=$(pgrep -f "$_supervisor_name")
}
_getPid
if [[ -z "$_pid" ]] ; then
echo "Not running"
exit
fi
log "Stop" "$_SV_CTRL_LOG_FILE"
echo "Stopping $_pid ..."
kill "$_pid"
_counter=0
while [[ ! -z "$_pid" ]]; do
sleep 1
_getPid
_counter=$((_counter+1))
if (( _counter >= 60 )); then
break
fi
done
if [[ ! -z "$_pid" ]]; then
echo "Still running. Now kill ..."
kill -9 "$_pid"
fi
exit 0
fi
#################################################################################
# Status
#################################################################################
if [[ "$_cmd" == "status" ]] ; then
if [[ -z "$(pgrep -f "$_supervisor_name")" ]] ; then
echo "Supervisor: Not running"
else
echo "Supervisor: Running"
fi
if [[ ! -z "$sv_final_program_pattern" ]] ; then
if [[ -z "$(pgrep -f "$sv_final_program_pattern")" ]] ; then
echo "Final program: Not running"
else
echo "Final program: Running"
fi
fi
exit 0
fi
#################################################################################
# Restart
#################################################################################
if [[ "$_cmd" == "restart" ]] ; then
log "Restart" "$_SV_CTRL_LOG_FILE"
"$_this_script" stop
"$_this_script" start
exit 0
fi
#################################################################################
# Show config
#################################################################################
if [[ "$_cmd" == "config" ]] ; then
cat "$_config_file"
exit 0
fi
echo "Invalid command"
exit 1