| 1 |
#![allow(clippy::expect_used)] |
| 2 |
#![allow(clippy::match_wild_err_arm)] |
| 3 |
|
| 4 |
|
| 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 |
|
| 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 |
|
| 22 |
Command::new("sleep") |
| 23 |
.arg("300") |
| 24 |
.output() |
| 25 |
.expect("failed to execute process"); |
| 26 |
} |
| 27 |
|
| 28 |
Ok(Fork::Parent(_)) => unreachable!("daemon exits parent processes"), |
| 29 |
Err(err) => eprintln!("daemon failed: {err}"), |
| 30 |
} |
| 31 |
} |