Why does bash exit immediately when waiting for a command to complete and receives SIGHUP for which a trap has been set?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite
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, 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?







share|improve this question

























    up vote
    2
    down vote

    favorite
    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, 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?







    share|improve this question























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      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, 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?







      share|improve this question













      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, 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?









      share|improve this question












      share|improve this question




      share|improve this question








      edited May 27 at 23:04
























      asked May 27 at 22:03









      Tim

      22.6k61222401




      22.6k61222401

























          active

          oldest

          votes











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f446361%2fwhy-does-bash-exit-immediately-when-waiting-for-a-command-to-complete-and-receiv%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f446361%2fwhy-does-bash-exit-immediately-when-waiting-for-a-command-to-complete-and-receiv%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay