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

#169 in Unix APIs

Download history 518/week @ 2020-08-06 327/week @ 2020-08-13 228/week @ 2020-08-20 317/week @ 2020-08-27 276/week @ 2020-09-03 365/week @ 2020-09-10 462/week @ 2020-09-17 168/week @ 2020-09-24 137/week @ 2020-10-01 137/week @ 2020-10-08 183/week @ 2020-10-15 251/week @ 2020-10-22 157/week @ 2020-10-29 345/week @ 2020-11-05 415/week @ 2020-11-12 360/week @ 2020-11-19

1,305 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

~740KB
~13K SLoC