| 1 | #![allow(clippy::expect_used)] |
| 2 | #![allow(clippy::match_wild_err_arm)] |
| 3 | |
| 4 | /// run with `cargo run --example example_daemon` |
| 5 | use std::process::Command; |
| 6 | |
| 7 | use fork::{Fork, daemon}; |
| 8 | |
| 9 | fn main() { |
| 10 | // Keep stdio open (noclose = true) so we can print the daemon PID |
| 11 | match daemon(false, true) { |
| 12 | Ok(Fork::Child) => { |
| 13 | println!("daemon pid: {}", std::process::id()); |
| 14 | Command::new("sleep") |
| 15 | .arg("300") |
| 16 | .output() |
| 17 | .expect("failed to execute process"); |
| 18 | } |
| 19 | // Parent exits inside daemon(); this arm is unreachable. |
| 20 | Ok(Fork::Parent(_)) => unreachable!("daemon exits parent processes"), |
| 21 | Err(err) => eprintln!("daemon failed: {err}"), |
| 22 | } |
| 23 | } |