Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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

2 Answers 2

up vote 1 down vote accepted

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

share|improve this answer
    
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 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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