0.1.0 |
|
---|
#12 in #daemonize
19KB
338 行
Daemonize-rs
示例
extern crate daemonize_rs;
use daemonize_rs::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);
}
}
}
许可证
本软件包根据GNU通用公共许可证版本3.0的条款分发。
有关详细信息,请参阅LICENSE。
依赖项
~0.7–1MB
~16K SLoC