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("sleep").arg("2").spawn().unwrap();
8
9    if let Some(id) = child.id() {
10        println!("Spawned process with ID: {id}");
11    }
12
13    // Do some work while the process runs
14    println!("Doing other work while process runs...");
15
16    sleep(Duration::from_secs(1));
17
18    // Check if the process has finished (non-blocking)
19    match child.try_wait().unwrap() {
20        Some(status) => println!("Process already finished with: {status}"),
21        None => println!("Process still running..."),
22    }
23
24    // Wait for the process to complete
25    println!("Waiting for process to finish...");
26    let output = child.wait().unwrap();
27
28    println!("Exit status: {}", output.status);
29}