1

From Bash manual

If Bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes.

When Bash is waiting for an asynchronous command via the wait builtin, the reception of a signal for which a trap has been set will cause the wait builtin to return immediately with an exit status greater than 128, immediately after which the trap is executed.

I created some experiments for the first sentence.

  1. In a shell running in a tab in lxterminal:

    $ trap "echo hello" HUP
    $ sleep 100
    

    I then close the tab window by clicking "x" icon, so that send SIGHUP to the shell.

    The shell exits immediately, instead of waiting for sleep 100 to complete, and then executing the trap. I was wondering why? Did I miss something?

  2. This one works

    $ trap -p
    trap -- 'echo hello' SIGHUP
    $ echo $$
    19708
    $ sleep 100
    

    Then run from another tab of lxterminal:

    $ kill -HUP 19708
    $
    

    When the sleep command in the first tab completes:

    $ sleep 100
    hello
    

Thanks.

Related When typing ctrl-c in a terminal, why isn't the foreground job terminated until it completes?

  • So I understand that , you opened a shell via ssh and did trap "echo hello" HUP and sleep 100 , then you closed that window with foreground process of sleep (by means of clicking the Close x mark or what is the means?) . Is my understanding correct ? – sai sasanka Sep 4 '18 at 6:10
  • I close the tab window by clicking "x" icon – Tim Sep 4 '18 at 9:56
0

In the first example, closing the terminal emulator window by clicking "x" icon will cause SIGHUP sent to the controlling terminal which is the shell.

The terminal emulator window is closed, but the shell doesn't exit andsleep 100 is still running until sleep 100 completes, and then the shell executes the SIGHUP trap. We can find that out from the output of ps, but not from the already closed terminal emulator window.

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.