Expand description

Small crate which helps with writing daemon applications in Rust.

I am aware about daemonize and daemonize-me crates, but needed some extended functionality like locking PID file and searching for running daemon.

Complete example:

use daemonizr::{Daemonizr, DaemonizrError, Group, Stderr, Stdout, User};
use std::{path::PathBuf, process::exit, thread::sleep, time::Duration};

fn main() {
    match Daemonizr::new()
        .expect("invalid path")
        .as_user(User::by_name("alex").expect("invalid user"))
        .as_group(Group::by_name("staff").expect("invalid group"))
        .expect("invalid umask")
        Err(DaemonizrError::AlreadyRunning) => {
            /* search for the daemon's PID  */
            match Daemonizr::new()
                Err(x) => eprintln!("error: {}", x),
                Ok(pid) => {
                    eprintln!("another daemon with pid {} is already running", pid);
        Err(e) => eprintln!("DaemonizrError: {}", e),
        Ok(()) => { /* We are in daemon process now */ }

    /* actual daemon work goes here */
    println!("write something to stdout");
    eprintln!("write something to stderr");
    println!("Daemon exits.")


⚠️ This crate will only work on POSIX compatible systems, where the “nix” and “libc” crates are available.


Daemonizr holds context needed for spawning the daemon process.


Error type reported by daemonizr.

Group object holds a valid group id (GID) to change to after child process has been daemonized.

Determines behaviour for “stderr” file descriptor

Determines behaviour for “stdout” file descriptor

User object holds a valid user id (UID) to change to after child process has been daemonized.