-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstop.sh
More file actions
executable file
·157 lines (133 loc) · 4.06 KB
/
Copy pathstop.sh
File metadata and controls
executable file
·157 lines (133 loc) · 4.06 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
#!/bin/bash
# Stop all LEDarcade running processes.
set -u
LEDARCADE_DIR="$(cd "$(dirname "$0")" && pwd)"
LOG_DIR="$LEDARCADE_DIR/localdata"
LOG_FILE="$LOG_DIR/update.log"
mkdir -p "$LOG_DIR"
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') [stop] $*" | tee -a "$LOG_FILE"
}
is_stop_script() {
local cmd="$1"
case "$cmd" in
*stop.sh*|*update_ledarcade.sh*)
return 0
;;
esac
return 1
}
matching_pids() {
local pattern="$1"
local pid cmd
for pid in $(pgrep -f "$pattern" 2>/dev/null || true); do
cmd=$(ps -p "$pid" -o args= 2>/dev/null || true)
if [ -z "$cmd" ] || is_stop_script "$cmd"; then
continue
fi
echo "$pid"
done
}
kill_pattern() {
local pattern="$1"
local label="$2"
local pid
if [ -z "$(matching_pids "$pattern")" ]; then
return 0
fi
log "Stopping $label"
pkill -f "$pattern" 2>/dev/null || true
if command -v sudo >/dev/null 2>&1; then
sudo pkill -f "$pattern" 2>/dev/null || true
fi
}
force_kill_pattern() {
local pattern="$1"
local label="$2"
if [ -z "$(matching_pids "$pattern")" ]; then
return 0
fi
log "Force stopping $label"
pkill -9 -f "$pattern" 2>/dev/null || true
if command -v sudo >/dev/null 2>&1; then
sudo pkill -9 -f "$pattern" 2>/dev/null || true
fi
}
still_running() {
local patterns=(
"python3.*$LEDARCADE_DIR"
"sudo -n python3.*$LEDARCADE_DIR"
"sudo python3.*$LEDARCADE_DIR"
"$LEDARCADE_DIR/start_ledarcade.sh"
"python3 (twitch|LEDcommander|arcade)\\.py"
"sudo(-n)? python3 (twitch|LEDcommander|arcade)\\.py"
)
local pattern
for pattern in "${patterns[@]}"; do
if [ -n "$(matching_pids "$pattern")" ]; then
return 0
fi
done
return 1
}
log "=== Stopping LEDarcade ==="
if screen -ls ledarcade 2>/dev/null | grep -q "[0-9]*\\.ledarcade"; then
log "Closing screen session 'ledarcade'"
screen -S ledarcade -X quit 2>/dev/null || true
fi
kill_pattern "$LEDARCADE_DIR/start_ledarcade.sh" "start_ledarcade.sh"
kill_pattern "start_ledarcade.sh --foreground" "start_ledarcade foreground wrapper"
kill_pattern "python3.*$LEDARCADE_DIR" "python3 (LEDarcade directory)"
kill_pattern "sudo -n python3.*$LEDARCADE_DIR" "sudo -n python3 (LEDarcade directory)"
kill_pattern "sudo python3.*$LEDARCADE_DIR" "sudo python3 (LEDarcade directory)"
LAUNCHERS=(
twitch.py
LEDcommander.py
arcade.py
particles.py
Defender.py
Defender2.py
Outbreak.py
Blasteroids.py
DotInvaders.py
FallingSand.py
gravitysim.py
Tron.py
SpaceDot.py
MazeCar.py
SpaceExplorer.py
StockTicker.py
AnalogClock.py
WeatherClock.py
LEDweb.py
LEDpanel.py
OnAir.py
RunningTest.py
demo.py
clock.py
)
for script in "${LAUNCHERS[@]}"; do
kill_pattern "python3 ${script}" "$script"
kill_pattern "python3 .*${LEDARCADE_DIR}/${script}" "$script (full path)"
kill_pattern "sudo python3 ${script}" "$script (sudo)"
kill_pattern "sudo -n python3 ${script}" "$script (sudo -n)"
done
sleep 2
if still_running; then
log "Some processes did not exit cleanly; sending SIGKILL"
force_kill_pattern "$LEDARCADE_DIR/start_ledarcade.sh" "start_ledarcade.sh"
force_kill_pattern "python3.*$LEDARCADE_DIR" "python3 (LEDarcade directory)"
force_kill_pattern "sudo -n python3.*$LEDARCADE_DIR" "sudo -n python3 (LEDarcade directory)"
force_kill_pattern "sudo python3.*$LEDARCADE_DIR" "sudo python3 (LEDarcade directory)"
force_kill_pattern "python3 (twitch|LEDcommander|arcade)\\.py" "launcher scripts"
force_kill_pattern "sudo(-n)? python3 (twitch|LEDcommander|arcade)\\.py" "sudo launcher scripts"
sleep 1
fi
if still_running; then
log "ERROR: Could not stop all LEDarcade processes. Try: sudo bash $LEDARCADE_DIR/stop.sh"
for pid in $(matching_pids "python3.*$LEDARCADE_DIR"); do
ps -p "$pid" -o pid=,args= 2>/dev/null | tee -a "$LOG_FILE" || true
done
exit 1
fi
log "All LEDarcade processes stopped"