example/
example.rs

1extern crate daemonize_me;
2
3use std::any::Any;
4use std::fs::File;
5use std::process::exit;
6
7pub use daemonize_me::Daemon;
8
9
10fn post_fork_parent(ppid: i32, cpid: i32) -> ! {
11    println!("Parent pid: {}, Child pid {}", ppid, cpid);
12    println!("Parent will keep running after the child is forked, might even go do other tasks");
13    println!("Or quit like so, bye :)");
14    exit(0);
15}
16
17fn post_fork_child(ppid: i32, cpid: i32) {
18    println!("Parent pid: {}, Child pid {}", ppid, cpid);
19    println!("This hook is called in the child");
20    // Child hook must return
21    return
22}
23
24fn after_init(_: Option<&dyn Any>) {
25    println!("Initialized the daemon!");
26    return
27}
28
29fn main() {
30    let stdout = File::create("info.log").unwrap();
31    let stderr = File::create("err.log").unwrap();
32    let daemon = Daemon::new()
33        .pid_file("example.pid", Some(false))
34        .umask(0o000)
35        .work_dir(".")
36        .stdout(stdout)
37        .stderr(stderr)
38        // Hooks are optional
39        .setup_post_fork_parent_hook(post_fork_parent)
40        .setup_post_fork_child_hook(post_fork_child)
41        .setup_post_init_hook(after_init, None)
42        // Start the daemon and calls the hooks
43        .start();
44
45    match daemon {
46        Ok(_) => println!("Daemonized with success"),
47        Err(e) => {
48            eprintln!("Error, {}", e);
49            exit(-1);
50        },
51    }
52
53    for i in 0..=10000 {
54        println!("{}", i);
55    }
56}