Use this tag for questions about the `trap` Bash built-in.
Questions tagged [bash-trap]
171 questions
0
votes
1 answer
How to run a closing script when issuing docker-compose stop or crtl-c
I'm doing:
docker-compose up
my service is essentially to run netlify dev
Because we may deal with several _redirects_xxx files, I have in my entrypoint.sh something like:
cleanup() {
if $remove; then
echo ">>> Removing created…

alanwilter
- 498
- 1
- 3
- 20
0
votes
2 answers
In bash shell -E option explanation, what does "any trap inherited by a subshell environment" mean?
This is from the manual for bash set options (for set -E)
-E
If set, any trap on ERR is inherited by shell functions, command substitutions, and commands executed in a subshell environment. The ERR trap is normally not inherited in such…

Chan Kim
- 5,177
- 12
- 57
- 112
0
votes
1 answer
Bash script pipe barrier
My current script looks like this:
#!/bin/bash
trap flush_buffer SIGUSR1
flush_buffer() {
echo "flushing buffer" >&2
[ -n "$buffer" ] && echo -e "$buffer"
buffer=""
}
trap exit_script INT TERM
exit_script() {
echo "script…

Julian
- 5
- 2
0
votes
1 answer
Bash trap command failing to catch fatal php script exit code 255
Minimal example of the issue:
#!/bin/bash
errHandler() {
echo "Something went wrong. Exiting now."
exit 1
}
trap 'errHandler' ERR INT
pi_process () {
bin/pi $@
echo $?
}
pi_process stuff
bin/pi is a php script, it's exiting with…

Shardj
- 1,800
- 2
- 17
- 43
0
votes
2 answers
How can i show error message for a particular command , if bash script terminates due to set -e
I want to display a error log line for one particular command when its return value is nonzero .
I am using ' set -e ' for terminating if any command returns nonzero value along with ' trap ' for this
#!/bin/bash
set -e
log_report() {
…

Pratheesh
- 565
- 4
- 19
0
votes
1 answer
In bash, how to catch the "event" that the program exits itself?
I have a bash program that needs to remove a lock file whenever it terminates.
It does not matter if the bash exits at the end of execution or if it failed at some point and exits with an error code, I want to catch this event and do some stuff…

Itération 122442
- 2,644
- 2
- 27
- 73
0
votes
1 answer
rsync suppress SIGINT for trap outside
I'm trying to build a generic retry shell function to re-run the specified shell command a few times if it fails in the last time, here is my code:
retry() {
declare -i number=$1
declare -i interrupt=0
trap "echo Exited!; interrupt=1;"…

Itachi
- 5,777
- 2
- 37
- 69
0
votes
1 answer
bash trap propagated to command with custom signal handler
In my script I'm trapping signals in the usual way.
function on_stop {
echo 'On Stop'
sleep 10
echo 'Signalling others to exit'
trap - TERM EXIT INT
kill -s INT "$$"
}
./executable_with_custom_signal_handling &
pid=$!
trap 'on_stop' TERM…

Yuki
- 3,857
- 5
- 25
- 43
0
votes
1 answer
How to disable ctrl-c in a non-interactive bash script and its git subprograms
I am calling git clone from a non-interactive bash shell. It is non-interactive because it is launched from a Windows Explorer contect menu. I am running git version 2.20.1.windows.1 on Windows 10 64-bit.
I am running Git/usr/bin/bash -l -e…

gitrdone
- 21
- 1
- 5
0
votes
1 answer
PySNMP Trap OID string value is getting converted to hex
I'm trying to write a script that sends a trap when a syslog message is received.
When i use a short literal string it works well, but when the string gets bigger or I use a string variable as the value of and OID, the trap is sent with the value…

amartiresST
- 3
- 3
0
votes
1 answer
bash trap will echo from keyboard Ctrl-C while not kill 2
Say I have a script:
#!/bin/bash
# test_trap.sh
trap "echo SIGINT captured!" SIGINT
echo $$
sleep 1000
I know trap COMMAND will only be executed after sleep 1000 finishes when it receives SIGINT signal. But the command of trap will be executed…

guaner
- 82
- 10
0
votes
2 answers
BASH - trap CTRL+C - Are you sure prompt doesn't always work
I've added the following to the top of my bash script:
quit() {
echo "Do you want to quit ? (y/n)"
read n
if [ "$n" = 'y' ]; then
exit
fi
}
trap quit INT
trap quit SIGINT
trap quit SIGTERM
The script asks the user a series of questions…

Tom
- 1,436
- 24
- 50
0
votes
1 answer
Possible to detect using trap command
I want to detect some command if they are used on my system. For that reason I use auditd on my Linux machine. But I can`t figure out if it is possible to detect the use of the trap command.
Maybe you can help me
Thanks a lot

tobmes
- 1
0
votes
2 answers
Is the term trap (of Bash) more accurate than the generic "callback" term in programming?
Please consider:
scripttmp=$(mktemp -d)
cleanup() {
rm -rf "${scripttmp}"
}
trap cleanup EXIT
I understand cleanup is a call(ed)back function, as it is being called just before exiting from the main function, from which it is part (I grasp…

Osi
- 1
- 3
- 9
- 30
0
votes
1 answer
bash trap with both optional and non-optional commands after error
I'm struggling to understand how to use bash's trap command correctly.
I have a script where I want to
Do A
Do B, which might fail
Whether B succeeded or failed, do C
If B failed, also do D
I think I get how to do 1-3. That'd look something…

Alex
- 2,555
- 6
- 30
- 48