Skip to content

Commit 2629357

Browse files
committed
Run systemctl poweroff with sudo
1 parent 2996d04 commit 2629357

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/main.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ fn start_monitoring(params: AuthMonitorParams) -> ExitCode {
7171
}
7272
}
7373

74-
const SYSTEMCTL_COMMAND: &str = "systemctl";
75-
const POWER_OFF_ARGS: [&str; 1] = ["poweroff"];
74+
const SUDO_COMMAND: &str = "sudo";
75+
const SYSTEMCTL_POWER_OFF_ARGS: [&str; 2] = ["systemctl", "poweroff"];
7676

7777
fn shutdown() {
78-
let output = match Command::new(SYSTEMCTL_COMMAND)
79-
.args(POWER_OFF_ARGS)
78+
let output = match Command::new(SUDO_COMMAND)
79+
.args(SYSTEMCTL_POWER_OFF_ARGS)
8080
.output()
8181
{
8282
Ok(output) => output,
@@ -85,8 +85,16 @@ fn shutdown() {
8585
return;
8686
}
8787
};
88-
match String::from_utf8(output.stdout) {
89-
Ok(stdout) => println!("Shutdown output: {}", stdout),
88+
let output_data = if output.status.success() {
89+
output.stdout
90+
} else {
91+
output.stderr
92+
};
93+
if output_data.is_empty() {
94+
return;
95+
}
96+
match String::from_utf8(output_data) {
97+
Ok(output_message) => println!("Shutdown output: {}", output_message),
9098
Err(error) => eprintln!("Error converting command output to string: {}", error),
9199
};
92100
}

0 commit comments

Comments
 (0)