Skip to content

Commit

Permalink
daemon examples
Browse files Browse the repository at this point in the history
nbari committed Jul 21, 2024

Verified

This commit was signed with the committer’s verified signature.
1 parent 8ea717a commit b3229f8
Showing 2 changed files with 48 additions and 0 deletions.
21 changes: 21 additions & 0 deletions examples/example_daemon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// run with `cargo run --example example_daemon`
use fork::{daemon, Fork};
use std::process::Command;

fn main() {
// Keep file descriptors open to print the pid of the daemon
match daemon(false, true) {
Ok(Fork::Child) => {
Command::new("sleep")
.arg("300")
.output()
.expect("failed to execute process");
}
Ok(Fork::Parent(pid)) => {
println!("daemon pid: {}", pid);
}
Err(_) => {
println!("Fork failed");
}
}
}
27 changes: 27 additions & 0 deletions examples/example_touch_pid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// run with `cargo run --example example_touch_pid`
use fork::{daemon, Fork};
use std::fs::OpenOptions;
use std::process::Command;

fn main() {
match daemon(false, false) {
Ok(Fork::Child) => {
Command::new("sleep")
.arg("300")
.output()
.expect("failed to execute process");
}
Ok(Fork::Parent(pid)) => {
// touch file with name like pid
let file_name = format!("/tmp/{}.pid", pid);
OpenOptions::new()
.write(true)
.create(true)
.open(file_name)
.expect("failed to open file");
}
Err(_) => {
println!("Fork failed");
}
}
}

0 comments on commit b3229f8

Please sign in to comment.