diff --git a/README.md b/README.md index 405f465..3fb1ee6 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Arguments: Options: -e, --err-path Path to write stderr to -o, --out-path Path to write stdout to + -p, --pid-file Path to the place to write a pidfile to --sig Signal notifiying that the file paths have been rotated [default: sighup] [possible values: sighup, sigusr1, sigusr2] -h, --help Print help -V, --version Print version @@ -28,5 +29,5 @@ Options: ## Example ```sh -clio --err-path /var/log/app.err.log --out-path /var/log/app.out.log --sig sighup -- app --flag1 --flag2 +clio --err-path /var/log/app.err.log --out-path /var/log/app.out.log --sig sighup --pid-file app.pid -- app --flag1 --flag2 ``` diff --git a/src/main.rs b/src/main.rs index a023702..6645854 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,12 +34,26 @@ struct Args { stderr_path: PathBuf, #[arg(short = 'o', long = "out-path", help = "Path to write stdout to")] stdout_path: PathBuf, + #[arg( + short = 'p', + long = "pid-file", + help = "Path to the place to write a pidfile to" + )] + pid_file: Option, #[arg(long = "sig", value_enum, help="Signal notifiying that the file paths have been rotated", default_value_t = HandledSignals::SIGHUP)] rotated_signal: HandledSignals, - #[arg(last = true, help="Command to run")] + #[arg(last = true, help = "Command to run")] cmd: Vec, } +async fn write_pid_file(p: &PathBuf) -> anyhow::Result<()> { + let mut pid_file = File::options().create(true).truncate(true).open(p).await?; + let id = std::process::id().to_string(); + pid_file.write(id.as_bytes()).await?; + pid_file.sync_all().await?; + Ok(()) +} + #[tokio::main] async fn main() -> anyhow::Result { let args = Args::parse(); @@ -61,16 +75,30 @@ async fn main() -> anyhow::Result { .stderr(Stdio::piped()) .spawn()?; let mut stdout_reader = child - .stdout.take() + .stdout + .take() .expect("no valid stdout from command available"); let mut stdout_buffer = [0; 8 * 1024]; let mut stderr_reader = child - .stderr.take() + .stderr + .take() .expect("no valid stderr from command available"); let mut stderr_buffer = [0; 8 * 1024]; - let mut stderr_writer = File::options().append(true).create(true).open(stderr_path).await?; - let mut stdout_writer = File::options().append(true).create(true).open(stdout_path).await?; + let mut stderr_writer = File::options() + .append(true) + .create(true) + .open(stderr_path) + .await?; + let mut stdout_writer = File::options() + .append(true) + .create(true) + .open(stdout_path) + .await?; + // TODO(jwall): Write our pidfile somehwere + if let Some(p) = args.pid_file { + write_pid_file(&p).await? + } // TODO(jwall): Forward all other signals to the running process. loop { // NOTE(zaphar): Each select block will run exclusively of the other blocks using a