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;
}
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
During the sleep command, I send a SIGINT
with kill -SIGINT <pid>
The result is different based on the PID receiving the signal
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
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
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
add a comment |
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
During the sleep command, I send a SIGINT
with kill -SIGINT <pid>
The result is different based on the PID receiving the signal
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
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
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
2
The error in case 2 is because you havetrap "SIGINT ...
rather thantrap "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, theecho done inner process
– icarus
1 hour ago
oh boy, thanks for pointing out themissing 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
add a comment |
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
During the sleep command, I send a SIGINT
with kill -SIGINT <pid>
The result is different based on the PID receiving the signal
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
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
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
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
During the sleep command, I send a SIGINT
with kill -SIGINT <pid>
The result is different based on the PID receiving the signal
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
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
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
shell-script trap
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 havetrap "SIGINT ...
rather thantrap "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, theecho done inner process
– icarus
1 hour ago
oh boy, thanks for pointing out themissing 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
add a comment |
2
The error in case 2 is because you havetrap "SIGINT ...
rather thantrap "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, theecho done inner process
– icarus
1 hour ago
oh boy, thanks for pointing out themissing 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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered 56 mins ago
icarusicarus
7,3191 gold badge17 silver badges33 bronze badges
7,3191 gold badge17 silver badges33 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
The error in case 2 is because you have
trap "SIGINT ...
rather thantrap "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