Git Product home page Git Product logo

fork's Introduction

fork

crates.io Build codecov docs

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

Why?

  • Minimal library to daemonize, fork, double-fork a process.
  • daemon(3) has been deprecated in MacOSX 10.5, by using fork and setsid syscalls, new methods can be created to achieve the same goal.

Example:

Create a new test project:

$ cargo new --bin myapp

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

Add fork as a depdendecie:

cargo add fork

or Edit myapp/Cargo.toml and add to [dependencies]:

fork = "0.1"

Add the following code to myapp/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 "myapp|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/myapp
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 myapp        PGID - 48737
      \--- 48753 sleep      PGID - 48737
    

fork's People

Contributors

anth0nyleung avatar kianmeng avatar nbari avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.