1

Is it possible and if so how do I:

  1. Start a /bin/bash process that is not bound to a terminal from a terminal? Hence, a shell process that shows up in the process tree as init -- bash. (Shells usually have the process tree structure terminal-emulator-of-your-choosing -- bash)
  2. Start a /bin/bash process not bound to a terminal that has as its child another process e.g. a browser like firefox from a shell session in a terminal? Hence, a shell process with a child that shows up in the process tree as init -- bash -- firefox.
  3. It is easy to get init -- firefox directly from the shell via something along the lines of exec firefox & exit or /bin/bash -c firefox & exit.

(This question is part of a series of related questions that first enabled me to formulate these questions precisely (cf. How to "correctly" start an application from a shell and Is reparenting from the shell possible?). The three questions have been formulated and discussed partially in the comments of the two former questions but as I see it not been answered. Furthermore, they do seem more appropriate as short question in their own right not suited for discussion in comments.)

lord.garbage
  • 2,373
  • 1
    Yes, yes and yes. There, now the "Is it possible" part is answered. How is another beast altogether. – Braiam Aug 28 '14 at 13:10
  • Yeah, I learned that the hard way... I tried and tried and searching the internet is not helpful either... – lord.garbage Aug 28 '14 at 13:34
  • 1
    Try setsid sh -c 'firefox&' <> /dev/null >&0 2>&0 (note that it makes it immune to SIGINTs) – Stéphane Chazelas Aug 28 '14 at 13:39
  • See also http://unix.stackexchange.com/q/68574/22565 – Stéphane Chazelas Aug 28 '14 at 13:46
  • And http://unix.stackexchange.com/a/56510/22565 – Stéphane Chazelas Aug 28 '14 at 13:49
  • 1
    Sorry setsid bash -c 'firefox; exit' if you actually want init -- bash -- firefox not connected to a terminal (setsid sh -c 'bash -c "firefox; exit" &' to make sure that bash doesn't get control of a terminal if it ever opens one). – Stéphane Chazelas Aug 28 '14 at 13:53
  • Hm, setsid making the process immune to SIGINTS worries me. Actually the link you provided contains a command that does exactly what I want: (trap '' HUP; command) &. Do you have an opinion on that (possible problems etc.?) – lord.garbage Aug 28 '14 at 13:59
  • In what way do your setsid commands make the process immune to SIGINTs? If i use kill -15 PID-of-firefox on the instances of firefox I started with your commands they exit cleanly. – lord.garbage Aug 28 '14 at 14:12
  • Or did you just mean immune to SIGINTS when I'm exiting the terminal? – lord.garbage Aug 28 '14 at 14:38
  • @lord.garbage (trap '' HUP; command) & still results in a process bound to a terminal (at least until the interactive shell you started it from exits). Is that what you want? – Mark Plotnick Aug 28 '14 at 17:07
  • What I want is complete when I have exited the shell just as you said. So the whole command I want is (trap '' HUP; command) & exit. – lord.garbage Aug 28 '14 at 17:42
  • @Stéphane, would you care to turn this into a concise answer? – lord.garbage Sep 01 '14 at 07:54
  • @lord.garbage, that's not setsid making processes immune to SIGINT (though it does make it immune to CTRL-C by detaching from the terminal). It's running a job in background from a subshell that does. The setsid() system call is the only way one can detach from a terminal. – Stéphane Chazelas Sep 01 '14 at 09:02
  • Do you need GNU bash? Because mksh has that feature built in. Run mksh -T/dev/tty10 to open one on that tty, mksh -T- -c 'command; command; …' to run dæmonised. (Full disclosure: I’m the mksh developer.) – mirabilos Dec 23 '14 at 18:52

0 Answers0