Skip to content

Commit ab2917d

Browse files
committed
Clean up shell PID monitoring
1 parent 8460bb2 commit ab2917d

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

rootfs/usr/local/sbin/shell-monitor

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@ wrapper_pids=()
66

77
# Function to count active shell sessions launched by wrapper
88
# Expensive, so run sparingly. Only needed to find new shells when they launch.
9-
count_shells() {
9+
shells_are_running() {
10+
# As a side effect, update the list of running shells
1011
wrapper_pids=($(list-wrapper-shells))
11-
echo "${#wrapper_pids[@]}"
12+
# Return true if there are any shells running
13+
[[ "${#wrapper_pids[@]}" -gt 0 ]]
1214
}
1315

1416
# Function to check if any registered shell has exited.
1517
# Super efficient, so run as often as needed.
1618
shell_has_exited() {
1719
local pid
1820

21+
trap 'set +x' EXIT
22+
set -x
23+
1924
# If there are no shells left, then they have all exited.
2025
[[ "${#wrapper_pids[@]}" -eq 0 ]] && return 0
2126

@@ -31,25 +36,25 @@ shell_has_exited() {
3136
# Function to kill all active shell sessions launched by wrapper.
3237
# This is the shutdown procedure, so we do not care about hogging the CPU.
3338
kill_shells() {
34-
for pid in $(list_wrapper_shells); do
39+
for pid in $(list-wrapper-shells); do
3540
kill -HUP $pid
3641
done
3742

3843
for i in {1..4}; do
39-
[ $(count_shells) -eq 0 ] && return 0
44+
shells_are_running || return 0
4045
sleep 1
4146
done
4247

43-
for pid in $(list_wrapper_shells); do
48+
for pid in $(list-wrapper-shells); do
4449
kill -TERM $pid
4550
done
4651

4752
for i in {1..3}; do
48-
[ $(count_shells) -eq 0 ] && return 0
53+
shells_are_running || return 0
4954
sleep 1
5055
done
5156

52-
for pid in $(list_wrapper_shells); do
57+
for pid in $(list-wrapper-shells); do
5358
kill -KILL $pid
5459
done
5560

@@ -62,7 +67,7 @@ trap 'kill_shells; exit $?' TERM HUP INT QUIT EXIT
6267
# Since we are waiting for something to happen, we can afford burn
6368
# up some CPU in order to be more responsive.
6469
i=0
65-
while [ $(count_shells) -eq 0 ]; do
70+
while ! shells_are_running; do
6671
sleep 0.5
6772
i=$((i + 1))
6873
if [ $i -ge 120 ]; then
@@ -87,7 +92,7 @@ while true; do
8792
while ! shell_has_exited; do
8893
sleep 0.67
8994
done
90-
[[ $(count_shells) -eq 0 ]] && break
95+
shells_are_running || break
9196
done
9297

9398
# Clean up and exit

0 commit comments

Comments
 (0)