Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

License

NotificationsYou must be signed in to change notification settings

immortal/fork

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

crates.ioBuildcodecovdocs

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 beendeprecated in MacOSX 10.5, by usingfork andsetsid syscalls, new methodscan be created to achieve the same goal.

Example:

Create a new test project:

$ cargo new --bin myapp

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

Addfork as a depdendecie:

cargo add fork

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

fork = "0.1"

Add the following code tomyapp/main.rs

use fork::{daemon,Fork};use std::process::Command;fnmain(){ifletOk(Fork::Child) =daemon(false,false){Command::new("sleep").arg("300").output().expect("failed to execute process");}}

If usingdaemon(false, false),it willchdir to/ and close the standard input, standard output, and standard error file descriptors.

Test running:

$ cargo run

Useps 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 theps 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/myapp48738 48753 48737      0 ??           0 S      501        0.0   0.0 sleep 300
  • PPID == 1 that's the parent process

  • TTY = ?? no controlling terminal

  • newPGID = 48737

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp