#fork #daemon #process #libc #setsid

fork

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

13 releases

✓ Uses Rust 2018 edition

new 0.1.12 Feb 21, 2020
0.1.11 Feb 21, 2020
0.1.10 Dec 2, 2019
0.1.9 Oct 28, 2019

#36 in Operating systems

Download history 12/week @ 2019-11-10 31/week @ 2019-11-17 51/week @ 2019-11-24 20/week @ 2019-12-01 78/week @ 2019-12-08 5/week @ 2019-12-15 14/week @ 2019-12-22 22/week @ 2020-01-05 113/week @ 2020-01-12 41/week @ 2020-01-19 1/week @ 2020-01-26 3/week @ 2020-02-02 19/week @ 2020-02-09 76/week @ 2020-02-16

130 downloads per month

Custom license

9KB
67 lines

fork

crates.io Build Status docs

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

Why?

Example:

Create a new test project:

$ cargo new --bin fork

To install cargo use: curl https://sh.rustup.rs -sSf | sh

Edit fork/Cargo.toml and add to [dependecies]:

fork = "0.1"

Add the following code to fork/main.rs

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

fn main() {
    if let Ok(Fork::Child) = daemon(false, false) {
        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.

Test running:

$ cargo run

Use ps to check the process, for example:

$ ps -axo ppid,pid,pgid,sess,tty,tpgid,stat,uid,%mem,%cpu,command, | egrep "fork|sleep|PID"

egrep is used to show the ps headers

Output should be something like:

 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
  • PPID == 1 that's the parent process

  • TTY = ?? no controlling terminal

  • new PGID = 48737

    1 - root (init/launchd)
     \-- 48738 fork         PGID - 48737
      \--- 48753 sleep      PGID - 48737
    

Dependencies

~25KB