From e9256e638979e506801b266c0cc1c7054ff2d7cd Mon Sep 17 00:00:00 2001 From: d4nn13 Date: Sun, 19 Apr 2026 19:49:12 +0200 Subject: [PATCH] detach service restarts from config-save CGI Saving wg0.conf via the web UI restarts wg-quick + watchdog + monitor serially. wg-quick's down/up can take long enough that busybox httpd closes the HTTP socket first, which the browser reports as "Failed to fetch" even though the file was saved. Move the restart loop into a detached background process, log its output to /var/log/wirecon-restart.log (with per-service timeout 20s), and have the CGI return the save confirmation immediately. The new log is also servable via logs.sh?service=restart. Co-Authored-By: Claude Opus 4.7 --- lxc/var/www/wirecon/cgi-bin/config-save.sh | 29 +++++++++++++++++----- lxc/var/www/wirecon/cgi-bin/logs.sh | 3 ++- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lxc/var/www/wirecon/cgi-bin/config-save.sh b/lxc/var/www/wirecon/cgi-bin/config-save.sh index 6f2154c..893d5c4 100755 --- a/lxc/var/www/wirecon/cgi-bin/config-save.sh +++ b/lxc/var/www/wirecon/cgi-bin/config-save.sh @@ -51,12 +51,29 @@ chmod "$mode" "$tmp" mv -f "$tmp" "$file" trap - EXIT -# Restart dependents, collect output for the UI. +# Return the save confirmation and kick off restarts in the background so +# the HTTP response doesn't depend on wg-quick / watchdog finishing. Their +# output is appended to /var/log/wirecon-restart.log (viewable in Logs tab +# or via tail -F). printf 'Content-Type: text/plain\r\n\r\n' echo "saved $file ($(wc -c < "$file") bytes)" +echo "" +echo "restarting in background:" +for svc in "${restart[@]}"; do echo " - $svc"; done +echo "" +echo "watch: tail -F /var/log/wirecon-restart.log" +echo "or refresh the Status tab in a few seconds." -for svc in "${restart[@]}"; do - echo "" - echo "--- restart $svc ---" - rc-service "$svc" restart 2>&1 || echo "(restart of $svc returned non-zero)" -done +{ + ts() { date '+%Y-%m-%dT%H:%M:%S%z'; } + echo "[$(ts)] config-save: $label ($(wc -c < "$file") bytes) → restarting ${restart[*]}" + for svc in "${restart[@]}"; do + echo "[$(ts)] --- restart $svc ---" + if ! timeout 20 rc-service "$svc" restart 2>&1; then + echo "[$(ts)] (restart of $svc returned non-zero or timed out)" + fi + done + echo "[$(ts)] config-save: done" +} >/var/log/wirecon-restart.log 2>&1 & +disown || true +exit 0 diff --git a/lxc/var/www/wirecon/cgi-bin/logs.sh b/lxc/var/www/wirecon/cgi-bin/logs.sh index b5d5261..566b01f 100755 --- a/lxc/var/www/wirecon/cgi-bin/logs.sh +++ b/lxc/var/www/wirecon/cgi-bin/logs.sh @@ -7,6 +7,7 @@ service=""; lines=100 case "${QUERY_STRING:-}" in *service=watchdog*) service=watchdog ;; *service=monitor*) service=monitor ;; + *service=restart*) service=restart ;; esac # pull lines= from query string @@ -17,7 +18,7 @@ fi if [[ -z "$service" ]]; then printf 'Status: 400 Bad Request\r\nContent-Type: text/plain\r\n\r\n' - echo "service must be watchdog or monitor"; exit 0 + echo "service must be watchdog, monitor, or restart"; exit 0 fi file="/var/log/wirecon-${service}.log"