Tips

how to stop bash killing jobs when you close a terminal

date added/updated
added 7/Oct/2002
updated 11/Mar/2007
keywords
bash HUP exit children jobs
example:
By default, when bash gets a HUP signal (such as when you close the terminal window that it is running in), it sends a HUP to all of the programs you have run from it. For many apps this causes them to exit. If you want to stop it sending this hup signal, read this whole page, including the warning below, then if you wish you may try adding the following command somewhere in your login files (eg your .bashrc):
trap "disown -h -a" HUP
This tells bash to run "disown -h -a" when it receives a HUP signal, and "disown -h -a" tells bash not to send a HUP to any of the children which are in it's job table at that time. Since we are running the disown command right before the shell exits, that will be all of it's jobs.

Unfortunately it also has the side effect of executing whatever is currently on the command line of the shell. I think this must be a bug in bash. For example, if you have the command 'emacs' typed in, but you haven't yet hit return, and then you close the window, the shell runs emacs and then waits for it to exit before exiting itself. If instead you have 'emacs &' typed in, it runs emacs and then exits straight away. So be warned - if you have a possibly dangerous command typed in, such as a rm command, this tip could do serious damage. I hereby disclaim any responsibility for accidents that may happen due to this problem. Use it at your own risk.

A few years later after I wrote this page, I found an interesting thread about it and discovered that my current version of bash doesn't do this any more... Here is that thread