1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
/// run with `cargo run --example example_touch_pid`
use fork::{Fork, daemon};
use std::fs::OpenOptions;
use std::process::Command;
fn main() {
match daemon(false, false) {
Ok(Fork::Child) => {
Command::new("sleep")
.arg("300")
.output()
.expect("failed to execute process");
}
Ok(Fork::Parent(pid)) => {
// touch file with name like pid
let file_name = format!("/tmp/{}.pid", pid);
OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(file_name)
.expect("failed to open file");
}
Err(_) => {
println!("Fork failed");
}
}
}
|