This is a followup to Do background processes get a SIGHUP when logging off?.

It seems that huponexit only works for login shells. How do I get that behavior for non-login shells, e.g. the ones that are started in my terminal window?

Sending HUP to the bash from where the processes have been started works as expected, but huponexit doesn't.

  • How about nohup, screen, tmux, ...? – quanta Nov 25 '11 at 18:46
  • I want the opposite behavior of nohup. – lxgr Nov 25 '11 at 19:29

You can declare your own code to run when the shell exits.

trap 'kill -HUP $(jobs -lp) 2>/dev/null || true' EXIT

(My earlier proposal of trap 'kill -HUP $$' EXIT doesn't work as of bash 4.2, because if I understand the source correctly bash skips part of its normal cleanup, including HUP resending, when it's already processing an exit condition.)

  • That doesn't seem to work - the kill doesn't seem to be sent to the child processes in the first place, since the shell isn't a login shell... – lxgr Nov 26 '11 at 15:42
  • @lxgr Ah, right. This doesn't work from a trap. Ok, then you can do it by sending the HUP signal explicitly. I've updated my answer. – Gilles 'SO- stop being evil' Nov 26 '11 at 17:02
trap 'kill -HUP -$$' exit

I can't seem to add a comment above. Prefix a negative sign to denote a process group. I think this will hang up all child processes spawned by the shell, include grandchildren, great-grandchildren, and so forth.

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.