File: example_daemon.rs

package info (click to toggle)
rust-fork 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 376 kB
  • sloc: makefile: 2
file content (23 lines) | stat: -rw-r--r-- 738 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#![allow(clippy::expect_used)]
#![allow(clippy::match_wild_err_arm)]

/// run with `cargo run --example example_daemon`
use std::process::Command;

use fork::{Fork, daemon};

fn main() {
    // Keep stdio open (noclose = true) so we can print the daemon PID
    match daemon(false, true) {
        Ok(Fork::Child) => {
            println!("daemon pid: {}", std::process::id());
            Command::new("sleep")
                .arg("300")
                .output()
                .expect("failed to execute process");
        }
        // Parent exits inside daemon(); this arm is unreachable.
        Ok(Fork::Parent(_)) => unreachable!("daemon exits parent processes"),
        Err(err) => eprintln!("daemon failed: {err}"),
    }
}