Debian Codegraph @

examples/example_touch_pid.rs

raw symbols SCIP
1 #![allow(clippy::expect_used)]
2 #![allow(clippy::match_wild_err_arm)]
3
4 /// run with `cargo run --example example_touch_pid`
5 use std::{fs::OpenOptions, process::Command};
6
7 use fork::{Fork, daemon};
8
9 fn main() {
10 match daemon(false, false) {
11 Ok(Fork::Child) => {
12 // Touch a PID file from the daemon process itself
13 let file_name = format!("/tmp/{}.pid", std::process::id());
14 OpenOptions::new()
15 .write(true)
16 .create(true)
17 .truncate(true)
18 .open(&file_name)
19 .expect("failed to open file");
20
21 // Do some work
22 Command::new("sleep")
23 .arg("300")
24 .output()
25 .expect("failed to execute process");
26 }
27 // Parent exits inside daemon(); this arm is unreachable.
28 Ok(Fork::Parent(_)) => unreachable!("daemon exits parent processes"),
29 Err(err) => eprintln!("daemon failed: {err}"),
30 }
31 }