Spawning two interactive shells as child processes causes parent to be stopped

⚓ rust    📅 2025-05-21    👤 surdeus    👁️ 4      

surdeus

Warning

This post was published 39 days ago. The information described in this article may have changed.

I have discovered some very peculiar behavior I don't understand.

I have a tool which needs to run various shells as child processes.

I have noticed that when I spawn two of these shells, the entire parent process and its children are stopped like if one would press ^Z.

fn main() {
    Command::new("sh")
        .args(["-i"])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();

    Command::new("sh")
        .args(["-i"])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();

    loop {}
}

When executed it prints

[1]+  Stopped                 cargo run

It can be resumed using fg.

This behavior is somehow caused by sh being invoked as interactive.

2 posts - 1 participant

Read full topic

🏷️ rust_feed