Module pid

Source
Expand description

§Pid

Pid is a Rust crate that provides functionality for creating, reading, and managing PID (Process ID) files.

A PID file typically contains the process ID of a running program. This crate offers an easy-to-use interface for working with PID files, allowing you to create new PID files, read process IDs from existing files, and handle the cleanup of PID files when they are no longer needed.

§Examples

Creating a new PID file:

use filelock_rs::pid::Pid;

match Pid::new("/var/run", "my_program") {
  Ok(pid) => {
     println!("PID file created. Process ID: {}", pid.process_id);
  }
  Err(error) => {
     eprintln!("Failed to create PID file: {}", error);
  }
}

Reading the process ID from an existing PID file:

use filelock_rs::pid::Pid;

let pid_file = Pid::new("/var/run", "my_program").expect("Failed to create PID file");
let process_id = pid_file.process_id;
println!("Process ID: {}", process_id);

§Cleanup

The Pid structure implements the Drop trait, ensuring that the PID file is automatically cleaned up when it goes out of scope. The file is unlocked and removed from the file system.

§Notes

  • The Pid crate uses the standard library’s file I/O and process ID functionality.
  • Ensure that the target directory has proper write permissions for creating and manipulating PID files.
  • If the PID file cannot be opened, locked, or written, an std::io::Error will be returned.

Structs§

Pid
Represents a PID (Process ID) file.