spawn/
spawn.rs

1use std::{thread::sleep, time::Duration};
2
3use privesc::PrivilegedCommand;
4
5fn main() {
6    // Spawn a privileged process without blocking
7    let mut child = PrivilegedCommand::new("/bin/sleep")
8        .arg("2")
9        .spawn()
10        .unwrap();
11
12    if let Some(id) = child.id() {
13        println!("Spawned process with ID: {id}");
14    }
15
16    // Do some work while the process runs
17    println!("Doing other work while process runs...");
18
19    sleep(Duration::from_secs(1));
20
21    // Check if the process has finished (non-blocking)
22    match child.try_wait().unwrap() {
23        Some(status) => println!("Process already finished with: {status}"),
24        None => println!("Process still running..."),
25    }
26
27    // Wait for the process to complete
28    println!("Waiting for process to finish...");
29    let output = child.wait().unwrap();
30
31    println!("Exit status: {}", output.status);
32}