Bash – How to Close a Terminal Without Killing Its Children

bashjob-controlkillprocessterminal

sometimes I run an app in the gnome-terminal, but then I suddenly have to restart gnome or something. I guess the answer to the question is also useful then I want to disconnect from SSH where something is happenning.

Gnome's terminal tree looks like this:

gnome-terminal
    bash
        some-boring-process

Can I 'detach' bash from gnome-terminal (or detach some-boring-process from bash and redirect its output somewhere)? If I just kill gnome-terminal, bash will be killed to will all its subprocesses

Best Answer

If some-boring-process is running in your current bash session:

  1. halt it with ctrl-z to give you the bash prompt
  2. put it in the background with bg
  3. note the job number, or use the jobs command
  4. detach the process from this bash session with disown -h %1 (substitute the actual job number there).

That doesn't do anything to redirect the output -- you have to think of that when you launch your boring process. [Edit] There seems to be a way to redirect it https://gist.github.com/782263

But seriously, look into screen. I have shells on a remote server that have been running for months.


Looks like this:

$ sleep 999999
^Z
[1]+  Stopped                 sleep 999999
$ bg
[1]+ sleep 999999 &
$ disown -h %1
Related Question