Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic sleep on mosquitto stop #1500

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions runs/atreboot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ chmod 666 "$LOGFILE"
fi
}

waitForServiceStop() {
# this function waits for a service to stop and kills the process if it takes too long
# this is necessary at least for mosquitto, as the service is stopped, but the process is still running
service=$1
pattern=$2
timeout=$3

counter=0
sudo systemctl stop "$service"
while pgrep --full "$pattern" >/dev/null && ((counter < timeout)); do
echo "process '$pattern' still running after ${counter}s, waiting..."
sleep 1
((counter++))
done
if ((counter >= timeout)); then
echo "process '$pattern' still running after ${timeout}s, killing process"
sudo pkill --full "$pattern" --signal 9
sleep 2
# if the process was killed, the service is in "active (exited)" state
# so we need to trigger a stop here to be able to start it again
sudo systemctl stop "$service"
fi
}

if ! id -u openwb >/dev/null 2>&1; then
echo "user 'openwb' missing"
echo "starting upgrade script..."
Expand Down Expand Up @@ -294,8 +318,7 @@ chmod 666 "$LOGFILE"
fi
if ((restartService == 1)); then
echo -n "restarting mosquitto service..."
sudo systemctl stop mosquitto
sleep 2
waitForServiceStop "mosquitto" "mosquitto.conf" 10
sudo systemctl start mosquitto
echo "done"
fi
Expand All @@ -318,8 +341,7 @@ chmod 666 "$LOGFILE"
fi
if ((restartService == 1)); then
echo -n "restarting mosquitto_local service..."
sudo systemctl stop mosquitto_local
sleep 2
waitForServiceStop "mosquitto_local" "mosquitto_local.conf" 10
sudo systemctl start mosquitto_local
echo "done"
fi
Expand Down