fork 0.1.5

Library for creating a new process detached from the controling terminal (daemon)
Documentation

fork

crates.io Build Status

Library for creating a new process detached from the controling terminal (daemon) using the fork and setsid syscalls.

Why?

Example:

use fork::{daemon, Fork};
use std::process::{id, Command};

fn main() {
    if let Ok(Fork::Child) = daemon(false, false) {
        println!("my pid {}", id());
        Command::new("sleep")
            .arg("300")
            .output()
            .expect("failed to execute process");
    }
}

If using daemon(false, false),it will chdir to / and close the standard input, standard output, and standard error file descriptors.

$ ps -axo ppid,pid,pgid,sess,tty,tpgid,stat,uid,%mem,%cpu,command, | egrep "fork|sleep|PID"
 PPID   PID  PGID   SESS TTY      TPGID STAT   UID       %MEM  %CPU COMMAND
    1 48738 48737      0 ??           0 S      501        0.0   0.0 target/debug/fork
48738 48753 48737      0 ??           0 S      501        0.0   0.0 sleep 300