File: example_touch_pid.rs

package info (click to toggle)
rust-fork 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 252 kB
  • sloc: makefile: 2
file content (28 lines) | stat: -rw-r--r-- 784 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/// run with `cargo run --example example_touch_pid`
use fork::{Fork, daemon};
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)
                .truncate(true)
                .open(file_name)
                .expect("failed to open file");
        }
        Err(_) => {
            println!("Fork failed");
        }
    }
}