- Notifications
You must be signed in to change notification settings - Fork39
Library for creating a new process detached from the controling terminal (daemon) using the fork and setsid syscalls.
License
immortal/fork
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Library for creating a new process detached from the controlling terminal (daemon).
- Minimal library to daemonize, fork, double-fork a process.
- daemon(3) has beendeprecated in MacOSX 10.5, by using
fork
andsetsid
syscalls, new methodscan 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
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 using
daemon(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 processTTY = ??
no controlling terminalnew
PGID = 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.