Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Trap SIGTERM and kill running process



On Apr 5,  8:34am, Thorsten Kampe wrote:
} 
} is it possible to trap SIGTERM and make sure that running processes 
} in the script (like `sleep 100`) will also be killed?

You can "setopt MONITOR HUP" and then zsh will send HUP to all the
jobs it has started whenever it exits, but that doesn't e.g. send the
TERM signal when zsh receives TERM.

} I'd rather terminate the group in the trap handler.

You can pass a negative process ID to zsh's builtin "kill" to send a
signal to the whole group (if supported by the OS).  So something
like this seems to work, though I haven't tested thoroughly:

    for sig in INT HUP TERM
    do
      trap "kill -$sig -$$" $sig 
    done
    trap "kill -HUP -$$" EXIT



Messages sorted by: Reverse Date, Date, Thread, Author