rust-shell-script / rust_cmd_lib

Common rust command-line macros and utilities, to write shell-script like tasks in a clean, natural and rusty way
https://docs.rs/cmd_lib/
Apache License 2.0
1.05k stars 37 forks source link

Support unix exec for replacing current process #61

Open neilyio opened 7 months ago

neilyio commented 7 months ago

It would be handy to be able to use Command::exec() with a convenient macro. I commonly use this when I need to transform the current process entirely into another process without creating a new subprocess. This can be particularly useful for creating wrappers around other programs or for use in certain types of daemon processes.

For example:

use std::os::unix::process::CommandExt;
use std::process::Command;

fn main() {
    let error = Command::new("ls")
        .arg("-l")
        .arg("/home")
        .exec();  // Replaces the current process with `ls`

    eprintln!("Failed to execute ls: {}", error);
}