Executing a background process with system()

13,054

Solution 1

Regardless of any ampersand to run it in the background, or what system() will do, you're launching an interactive shell. When you launch an interactive shell, it looks for a console to connect to, Failing that, it looks for stdin lines to process. Failing that, it exits. That's what's happening.

Solution 2

Following code works perfectly, using htop I can see that sleep is still running after my app terminates. I don't see how it should be different in your code.

#include <stdio.h>

int main()
{ 
   return system("sleep 100 &");
} 

Solution 3

It's because zsh and all shells bind stdin and it couldn't on background so it crashed. That's also why sleep in background worked.

Share:
13,054
node ninja
Author by

node ninja

Updated on June 04, 2022

Comments

  • node ninja
    node ninja almost 2 years

    I tried to execute a process using something like the following:

    system("zsh &");

    I don't think it works though, because the process doesn't show up. Why doesn't it work? How should it be changed?