trap command not foundCan I trap a clock signal in my bash script?Prevent SIGINT from interrupting function...

First amendment and employment: Can an police department terminate an officer for speech?

How to mark beverage cans in a cooler for a blind person?

Infeasibility in mathematical optimization models

Do other countries guarantee freedoms that the United States does not have?

What word can be used to describe a bug in a movie?

Is it really ~648.69 km/s delta-v to "land" on the surface of the Sun?

Double blind peer review when paper cites author's GitHub repo for code

How do I explain to a team that the project they will work on for six months will certainly be cancelled?

How to use grep to search through the --help output?

Max Order of an Isogeny Class of Rational Elliptic Curves is 8?

Ex-contractor published company source code and secrets online

Why isn’t SHA-3 in wider use?

How can I tell if a flight itinerary is fake?

Three legged NOT gate? What is this symbol?

Are any jet engines used in combat aircraft water cooled?

Dropdowns & Chevrons for Right to Left languages

How can you evade tax by getting employment income just in equity, then using this equity as collateral to take out loan?

'sudo apt-get update' get a warning

Generator for parity?

If a Contingency spell has been cast on a creature, does the Simulacrum spell transfer the contingent spell to its duplicate?

How would I as a DM create a smart phone-like spell/device my players could use?

Y2K... in 2019?

Best gun to modify into a monsterhunter weapon?

Dereferencing a pointer in a 'for' loop initializer creates a segmentation fault



trap command not found


Can I trap a clock signal in my bash script?Prevent SIGINT from interrupting function call and child process(es) withinCorrect behavior of EXIT and ERR traps when using `set -eu`dash: How to capture output of trap (invoked w/o arguments)?Why does bash exit immediately when waiting for a command to complete and receives SIGHUP for which a trap has been set?exceptions to ERR trap






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







-1















I run into this weird behavior of trap.



According to trap's manpage:




When a subshell is entered, traps that are not being ignored are set to the default actions. This does not imply that the trap command cannot be used within the subshell to set new traps.




This is how I interpret it:




  • If a script traps signal A, its subshell will also trap signal A but with a default action.

  • The subshell can also specify another action to trap that same signal


To test my understanding, I have two scripts:



outerscript.sh



#!/bin/bash

trap "echo SIGINT in outer" SIGINT
echo PID of outer process: $$
echo -----------;
./innerscript.sh


innerscript.sh



#!/bin/bash

echo start inner script
echo PID of inner process: $$
trap "SIGINT in inner, do graceful shutdown" SIGINT
sleep 10s
echo done inner process


Then I run the outerscript with ./outerscript.sh
The outerscript will then call the innerscript and create a subshell as shown in the picture



enter image description here



During the sleep command, I send a SIGINT with kill -SIGINT <pid>



The result is different based on the PID receiving the signal





  1. PID to receive signal is that of outerscript



    Note that I still have to wait for the sleep command to complete



    done inner process
    SIGINT in outer



  2. PID to receive signal is that of innerscript



    Note that I still have to wait for the sleep command to complete



    SIGINT: command not found
    done inner process



  3. PID to receive signal is that of sleep



    Note that I DON'T have to wait for the sleep command to complete



    done inner process



Question



In case 2), why do I get that error? I expected innerscript to trigger its trap function.



In case 3), why does it even trigger innerscript's trap function? I expected it to just kill itself and return normally to the inner script process.










share|improve this question






















  • 2





    The error in case 2 is because you have trap "SIGINT ... rather than trap "echo SIGINT ...

    – icarus
    1 hour ago






  • 2





    In case 3 it is not triggering the trap function, it is stopping the sleep, then going onto the next line, the echo done inner process

    – icarus
    1 hour ago











  • oh boy, thanks for pointing out the missing echo function. I can't believe I struggled with this for hours.

    – Tran Triet
    1 hour ago











  • If you could post this as an answer, I'd gladly mark it as the solution.

    – Tran Triet
    1 hour ago











  • A fresh pair of eyes often helps!

    – icarus
    55 mins ago


















-1















I run into this weird behavior of trap.



According to trap's manpage:




When a subshell is entered, traps that are not being ignored are set to the default actions. This does not imply that the trap command cannot be used within the subshell to set new traps.




This is how I interpret it:




  • If a script traps signal A, its subshell will also trap signal A but with a default action.

  • The subshell can also specify another action to trap that same signal


To test my understanding, I have two scripts:



outerscript.sh



#!/bin/bash

trap "echo SIGINT in outer" SIGINT
echo PID of outer process: $$
echo -----------;
./innerscript.sh


innerscript.sh



#!/bin/bash

echo start inner script
echo PID of inner process: $$
trap "SIGINT in inner, do graceful shutdown" SIGINT
sleep 10s
echo done inner process


Then I run the outerscript with ./outerscript.sh
The outerscript will then call the innerscript and create a subshell as shown in the picture



enter image description here



During the sleep command, I send a SIGINT with kill -SIGINT <pid>



The result is different based on the PID receiving the signal





  1. PID to receive signal is that of outerscript



    Note that I still have to wait for the sleep command to complete



    done inner process
    SIGINT in outer



  2. PID to receive signal is that of innerscript



    Note that I still have to wait for the sleep command to complete



    SIGINT: command not found
    done inner process



  3. PID to receive signal is that of sleep



    Note that I DON'T have to wait for the sleep command to complete



    done inner process



Question



In case 2), why do I get that error? I expected innerscript to trigger its trap function.



In case 3), why does it even trigger innerscript's trap function? I expected it to just kill itself and return normally to the inner script process.










share|improve this question






















  • 2





    The error in case 2 is because you have trap "SIGINT ... rather than trap "echo SIGINT ...

    – icarus
    1 hour ago






  • 2





    In case 3 it is not triggering the trap function, it is stopping the sleep, then going onto the next line, the echo done inner process

    – icarus
    1 hour ago











  • oh boy, thanks for pointing out the missing echo function. I can't believe I struggled with this for hours.

    – Tran Triet
    1 hour ago











  • If you could post this as an answer, I'd gladly mark it as the solution.

    – Tran Triet
    1 hour ago











  • A fresh pair of eyes often helps!

    – icarus
    55 mins ago














-1












-1








-1








I run into this weird behavior of trap.



According to trap's manpage:




When a subshell is entered, traps that are not being ignored are set to the default actions. This does not imply that the trap command cannot be used within the subshell to set new traps.




This is how I interpret it:




  • If a script traps signal A, its subshell will also trap signal A but with a default action.

  • The subshell can also specify another action to trap that same signal


To test my understanding, I have two scripts:



outerscript.sh



#!/bin/bash

trap "echo SIGINT in outer" SIGINT
echo PID of outer process: $$
echo -----------;
./innerscript.sh


innerscript.sh



#!/bin/bash

echo start inner script
echo PID of inner process: $$
trap "SIGINT in inner, do graceful shutdown" SIGINT
sleep 10s
echo done inner process


Then I run the outerscript with ./outerscript.sh
The outerscript will then call the innerscript and create a subshell as shown in the picture



enter image description here



During the sleep command, I send a SIGINT with kill -SIGINT <pid>



The result is different based on the PID receiving the signal





  1. PID to receive signal is that of outerscript



    Note that I still have to wait for the sleep command to complete



    done inner process
    SIGINT in outer



  2. PID to receive signal is that of innerscript



    Note that I still have to wait for the sleep command to complete



    SIGINT: command not found
    done inner process



  3. PID to receive signal is that of sleep



    Note that I DON'T have to wait for the sleep command to complete



    done inner process



Question



In case 2), why do I get that error? I expected innerscript to trigger its trap function.



In case 3), why does it even trigger innerscript's trap function? I expected it to just kill itself and return normally to the inner script process.










share|improve this question
















I run into this weird behavior of trap.



According to trap's manpage:




When a subshell is entered, traps that are not being ignored are set to the default actions. This does not imply that the trap command cannot be used within the subshell to set new traps.




This is how I interpret it:




  • If a script traps signal A, its subshell will also trap signal A but with a default action.

  • The subshell can also specify another action to trap that same signal


To test my understanding, I have two scripts:



outerscript.sh



#!/bin/bash

trap "echo SIGINT in outer" SIGINT
echo PID of outer process: $$
echo -----------;
./innerscript.sh


innerscript.sh



#!/bin/bash

echo start inner script
echo PID of inner process: $$
trap "SIGINT in inner, do graceful shutdown" SIGINT
sleep 10s
echo done inner process


Then I run the outerscript with ./outerscript.sh
The outerscript will then call the innerscript and create a subshell as shown in the picture



enter image description here



During the sleep command, I send a SIGINT with kill -SIGINT <pid>



The result is different based on the PID receiving the signal





  1. PID to receive signal is that of outerscript



    Note that I still have to wait for the sleep command to complete



    done inner process
    SIGINT in outer



  2. PID to receive signal is that of innerscript



    Note that I still have to wait for the sleep command to complete



    SIGINT: command not found
    done inner process



  3. PID to receive signal is that of sleep



    Note that I DON'T have to wait for the sleep command to complete



    done inner process



Question



In case 2), why do I get that error? I expected innerscript to trigger its trap function.



In case 3), why does it even trigger innerscript's trap function? I expected it to just kill itself and return normally to the inner script process.







shell-script trap






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 min ago









Kusalananda

159k18 gold badges313 silver badges500 bronze badges




159k18 gold badges313 silver badges500 bronze badges










asked 1 hour ago









Tran TrietTran Triet

25011 bronze badges




25011 bronze badges











  • 2





    The error in case 2 is because you have trap "SIGINT ... rather than trap "echo SIGINT ...

    – icarus
    1 hour ago






  • 2





    In case 3 it is not triggering the trap function, it is stopping the sleep, then going onto the next line, the echo done inner process

    – icarus
    1 hour ago











  • oh boy, thanks for pointing out the missing echo function. I can't believe I struggled with this for hours.

    – Tran Triet
    1 hour ago











  • If you could post this as an answer, I'd gladly mark it as the solution.

    – Tran Triet
    1 hour ago











  • A fresh pair of eyes often helps!

    – icarus
    55 mins ago














  • 2





    The error in case 2 is because you have trap "SIGINT ... rather than trap "echo SIGINT ...

    – icarus
    1 hour ago






  • 2





    In case 3 it is not triggering the trap function, it is stopping the sleep, then going onto the next line, the echo done inner process

    – icarus
    1 hour ago











  • oh boy, thanks for pointing out the missing echo function. I can't believe I struggled with this for hours.

    – Tran Triet
    1 hour ago











  • If you could post this as an answer, I'd gladly mark it as the solution.

    – Tran Triet
    1 hour ago











  • A fresh pair of eyes often helps!

    – icarus
    55 mins ago








2




2





The error in case 2 is because you have trap "SIGINT ... rather than trap "echo SIGINT ...

– icarus
1 hour ago





The error in case 2 is because you have trap "SIGINT ... rather than trap "echo SIGINT ...

– icarus
1 hour ago




2




2





In case 3 it is not triggering the trap function, it is stopping the sleep, then going onto the next line, the echo done inner process

– icarus
1 hour ago





In case 3 it is not triggering the trap function, it is stopping the sleep, then going onto the next line, the echo done inner process

– icarus
1 hour ago













oh boy, thanks for pointing out the missing echo function. I can't believe I struggled with this for hours.

– Tran Triet
1 hour ago





oh boy, thanks for pointing out the missing echo function. I can't believe I struggled with this for hours.

– Tran Triet
1 hour ago













If you could post this as an answer, I'd gladly mark it as the solution.

– Tran Triet
1 hour ago





If you could post this as an answer, I'd gladly mark it as the solution.

– Tran Triet
1 hour ago













A fresh pair of eyes often helps!

– icarus
55 mins ago





A fresh pair of eyes often helps!

– icarus
55 mins ago










1 Answer
1






active

oldest

votes


















2














There are 2 questions.
The error in case 2 is because there is no command called SIGINT on the system. The OP had



trap "SIGINT in inner, do graceful shutdown" SIGINT


but probably the desired command was



trap "echo SIGINT in inner, do graceful shutdown" SIGINT


The other question asks why the inner scripts trap function is invoked, however it is not. The sleep process is killed, the script moves onto the next line.






share|improve this answer




























    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    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%2f534961%2ftrap-command-not-found%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    There are 2 questions.
    The error in case 2 is because there is no command called SIGINT on the system. The OP had



    trap "SIGINT in inner, do graceful shutdown" SIGINT


    but probably the desired command was



    trap "echo SIGINT in inner, do graceful shutdown" SIGINT


    The other question asks why the inner scripts trap function is invoked, however it is not. The sleep process is killed, the script moves onto the next line.






    share|improve this answer






























      2














      There are 2 questions.
      The error in case 2 is because there is no command called SIGINT on the system. The OP had



      trap "SIGINT in inner, do graceful shutdown" SIGINT


      but probably the desired command was



      trap "echo SIGINT in inner, do graceful shutdown" SIGINT


      The other question asks why the inner scripts trap function is invoked, however it is not. The sleep process is killed, the script moves onto the next line.






      share|improve this answer




























        2












        2








        2







        There are 2 questions.
        The error in case 2 is because there is no command called SIGINT on the system. The OP had



        trap "SIGINT in inner, do graceful shutdown" SIGINT


        but probably the desired command was



        trap "echo SIGINT in inner, do graceful shutdown" SIGINT


        The other question asks why the inner scripts trap function is invoked, however it is not. The sleep process is killed, the script moves onto the next line.






        share|improve this answer













        There are 2 questions.
        The error in case 2 is because there is no command called SIGINT on the system. The OP had



        trap "SIGINT in inner, do graceful shutdown" SIGINT


        but probably the desired command was



        trap "echo SIGINT in inner, do graceful shutdown" SIGINT


        The other question asks why the inner scripts trap function is invoked, however it is not. The sleep process is killed, the script moves onto the next line.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 56 mins ago









        icarusicarus

        7,3191 gold badge17 silver badges33 bronze badges




        7,3191 gold badge17 silver badges33 bronze badges

































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f534961%2ftrap-command-not-found%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

            Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

            Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...