3

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?

node ninja
  • 31,796
  • 59
  • 166
  • 254
  • Doesn't `&` imply background, hence it should not show up isn't it? Have you checked jobs running in the background? – Sadique Apr 17 '11 at 02:31
  • What is the actual command string you are passing to system? Whatever you pass is given to the system's shell (typically /bin/sh), so you might try testing with `/bin/sh -c "your command &"`. – John Zwinck Apr 17 '11 at 02:32

3 Answers3

3

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.

dj_segfault
  • 11,957
  • 4
  • 29
  • 37
1

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 &");
} 
Konstantin Weitz
  • 6,180
  • 8
  • 26
  • 43
1

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.

Daniel
  • 30,896
  • 18
  • 85
  • 139