fix(shutdown): Fix shutdown command

This commit is contained in:
Björn Benouarets
2025-11-11 20:38:49 +01:00
parent 8ed56f7ba0
commit b8964d7763
4 changed files with 115 additions and 21 deletions

View File

@@ -4,15 +4,33 @@
package system
import (
"log"
"os/exec"
"git.secnex.io/secnex/masterlog"
)
// Shutdown shuts down the system (Unix/Linux/macOS)
func Shutdown() {
// Try shutdown command first
cmd := exec.Command("shutdown", "-h", "now")
if err := cmd.Run(); err != nil {
log.Fatalf("Failed to shutdown system: %v", err)
masterlog.Error("Failed to shutdown system (trying with sudo)", map[string]interface{}{
"error": err,
})
// Try with sudo if the first attempt failed
cmd = exec.Command("sudo", "shutdown", "-h", "now")
if err := cmd.Run(); err != nil {
masterlog.Error("Failed to shutdown system even with sudo", map[string]interface{}{
"error": err,
"note": "The application may need to be run with sudo privileges",
})
// Don't use log.Fatalf here, just log the error
// The system might still shutdown or the user can manually shutdown
return
}
}
masterlog.Info("Shutdown command executed successfully")
}

View File

@@ -4,8 +4,9 @@
package system
import (
"log"
"os/exec"
"git.secnex.io/secnex/masterlog"
)
// Shutdown shuts down the system (Windows)
@@ -13,6 +14,12 @@ func Shutdown() {
cmd := exec.Command("shutdown", "/s", "/t", "0")
if err := cmd.Run(); err != nil {
log.Fatalf("Failed to shutdown system: %v", err)
masterlog.Error("Failed to shutdown system", map[string]interface{}{
"error": err,
"note": "The application may need administrator privileges",
})
return
}
masterlog.Info("Shutdown command executed successfully")
}