37 lines
933 B
Go
37 lines
933 B
Go
//go:build !windows
|
|
// +build !windows
|
|
|
|
package system
|
|
|
|
import (
|
|
"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 {
|
|
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")
|
|
}
|