File: example_daemon.rs

package info (click to toggle)
rust-fork 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 252 kB
  • sloc: makefile: 2
file content (21 lines) | stat: -rw-r--r-- 559 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
/// run with `cargo run --example example_daemon`
use fork::{Fork, daemon};
use std::process::Command;

fn main() {
    // Keep file descriptors open to print the pid of the daemon
    match daemon(false, true) {
        Ok(Fork::Child) => {
            Command::new("sleep")
                .arg("300")
                .output()
                .expect("failed to execute process");
        }
        Ok(Fork::Parent(pid)) => {
            println!("daemon pid: {}", pid);
        }
        Err(_) => {
            println!("Fork failed");
        }
    }
}