4 releases (2 breaking)

0.3.0 Feb 14, 2019
0.2.1 Feb 14, 2019
0.2.0 Feb 4, 2019
0.1.1 Dec 18, 2018
0.1.0 Dec 11, 2018

#11 in #daemonize

Download history 75/week @ 2024-07-21 67/week @ 2024-07-28 60/week @ 2024-08-04 74/week @ 2024-08-11 72/week @ 2024-08-18 62/week @ 2024-08-25 47/week @ 2024-09-01 48/week @ 2024-09-08 68/week @ 2024-09-15 87/week @ 2024-09-22 67/week @ 2024-09-29 61/week @ 2024-10-06 167/week @ 2024-10-13 108/week @ 2024-10-20 52/week @ 2024-10-27 47/week @ 2024-11-03

377 downloads per month

GPL-3.0 license

20KB
376 lines

Daemonize-rs Crates.io Released API docs Build Status Build Status

Example

extern crate parity_daemonize;

use parity_daemonize::daemonize;
use std::{thread, time, process, io};
use io::Write;

fn main() {
    match daemonize("pid_file.txt") {
        // we are now in the daemon, use this handle to detach from the parent process
        Ok(handle) => {
            let mut count = 0;
            loop {
                // the daemon's output is piped to the parent process' stdout
                println!("Count: {}", count);
                if count == 5 {
                    handle.detach_with_msg("count has reached 5, continuing in background");
                }
                thread::sleep(time::Duration::from_secs(1));
                count += 1;
            }
        }
        // the daemon or the parent process may receive this error,
        // just print it and exit
        Err(e) => {
            // if this is the daemon, this is piped to the parent's stderr
            eprintln!("{}", e);
            // don't forget to flush
            io::stderr().flush();
            process::exit(1);
        }
    }
}

License

This crate is distributed under the terms of GNU GENERAL PUBLIC LICENSE version 3.0.

See LICENSE for details.

Dependencies

~0.7–1MB
~16K SLoC