I have a process running the foreground. I am wondering if it's possible to exit Bash,

  1. without (of course) killing the foreground process and exiting as usual, and
  2. without killing the terminal program, or if the connection is remote via a client (like iTerm2 or PuTTY), without killing the said client.

I know if I don't have a foreground process running, I can readily send End of Transmission via Ctrl-d or issuing exit. I'd like to know if this is possible with a foreground process running.

  • Is your goal to be able to log out without terminating a shell that has some program running in the foreground? Have you considered using GNU screen or tmux? – Kusalananda May 27 '19 at 6:46
  • My goal was more academic...I was looking into the shopt huponexit; I believe this option affects both foreground and background processes. I was trying to verify this indeed works on the foreground process - but for this, I need a way to exit without terminating, hence the (rather admittedly curious) question. – flow2k May 27 '19 at 8:13

Foreground process meant to server below properties, and those are tied with terminal.

  • to interact with the user.
  • to feed text input to the master pseudo-device for use by the shell (such as bash), which is connected to the slave pseudo-device.
  • to read text output from the master pseudo-device and show it to the user.

So i think this is not possible. Or run your process as daemon and feed output to particular terminal and/or file like installation process does.

  • I'm trying to understand "feed output to particular terminal and/or file like installation process does" - are you referring to redirecting stdout to a file? Is there something that explains this more? – flow2k May 27 '19 at 4:59

In addition to screen/tmux there's also nohup. It is simpler but sometimes better depending on your exact situation. See here: nohup vs. Screen for more details.

Note that nohup would keep the process in foreground but you won't be able to interact with it or see the outputs on the terminal. To start the process in background add & when starting the process (i.e. nohup <your command here> &).

If the process is alrady running with nohup you can re-gain access to the terminal by pressing Ctrl+Z (stop process) followed by bg command (send stopped process to background).

EDIT (after OP comment): I'm not sure I understand the scenario but maybe you could try that:

  1. open terminal window (some shell will start inside)
  2. explicitly start another shell (e.g. bash) inside that window
  3. start your foreground process inside the second shell (without any nohups etc)
  4. open another terminal window
  5. identify the "middle" shell (it will be the parent process of the tested process)
  6. send kill signal to the "middle" shell.

This turns the tested process into a "semi-daemon" and gives you access to the top level shell in the first terminal window. Note that the tested process is still attached to the terminal so it will be terminated when you close the window.

  • Thanks skazi for this info; please see my comment under the question for some additional context, which I probably should have included in the original post. – flow2k May 27 '19 at 8:16
  • Regarding the comment in your last paragraph, you can always use Ctrl-z to stop the process in the foreground - this is not restricted to processes started by nohup, right? – flow2k May 27 '19 at 8:28
  • Generally yes, you can stop the process and send it to background but if it was started without nohup then it will be killed when you close the terminal (just like if it was running in the foreground). – skazi May 27 '19 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.