2

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.

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

2 Answers 2

1

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.)

2
  • 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, 2011 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. Nov 26, 2011 at 17:02
1
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .