Restart script re-execs itself with sudo when not run as root

The script restarts system services via systemctl, which requires root.
Previously it would only fail per-command when run by an unprivileged
user. Add an EUID guard at the top that re-execs the script through
sudo, preserving the original path and arguments.
This commit is contained in:
2026-08-20 23:05:54 +00:00
parent 94705490b9
commit a77cee821b
+7
View File
@@ -1,5 +1,10 @@
#!/bin/bash #!/bin/bash
if [[ $EUID -ne 0 ]]; then
exec sudo "$0" "$@"
exit 0
fi
echo "systemctl restart nginx" echo "systemctl restart nginx"
systemctl restart nginx systemctl restart nginx
sleep 1 sleep 1
@@ -22,3 +27,5 @@ if [ "$failed" -eq 1 ]; then
exit 1 exit 1
fi fi
exit 0