rust_cmd_lib icon indicating copy to clipboard operation
rust_cmd_lib copied to clipboard

Support unix exec for replacing current process

Open neilyio opened this issue 1 year ago • 0 comments

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);
}

neilyio avatar Apr 12 '24 16:04 neilyio