Questions tagged [bash-trap]

Use this tag for questions about the `trap` Bash built-in.

171 questions
7
votes
1 answer

bash trap of TERM - what am I doing wrong?

Given this hack.c program: #include main() { int i=0; for(i=0; i<100; i++) { printf("%d\n", i); sleep(5); } } and this hack.sh bash script: #!/bin/bash ./hack If I run hack.sh, two processes get created - one for bash, one for…
user185094
6
votes
2 answers

Reliably kill sleep process after USR1 signal

I am writing a shell script which performs a task periodically and on receiving a USR1 signal from another process. The structure of the script is similar to this answer: #!/bin/bash trap 'echo "doing some work"' SIGUSR1 while : do sleep 10…
jameh
  • 1,149
  • 3
  • 12
  • 20
6
votes
3 answers

Trying to close all child processes when I interrupt my bash script

I have written a bash script to carry out some tests on my system. The tests run in the background and in parallel. The tests can take a long time and sometimes I may wish to abort the tests part way through. If I Control+C then it aborts the parent…
aghsmith
  • 265
  • 3
  • 11
6
votes
2 answers

ctrl+c to kill a bash script with child processes

I have a script whose internals boil down to: trap "exit" SIGINT SIGTERM while : do mplayer sound.mp3 sleep 3 done (yes, it is a bit more meaningful than the above, but that's not relevant to the problem). Several instances of the script…
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
6
votes
1 answer

Bash: Trap ERR does not work when pipe operator is used

I am trying to log everything that comes out of stdout and stderr into a log file and still preserve the console. For this, I just appended: |& tee -a log_file.log to every command. However, I also want to run a custom command if any error occurred…
cristiprg
  • 103
  • 1
  • 9
5
votes
3 answers

Bash, CTRL+C in eval not interrupting the main script

In my bash script, I'm running an external command that's stored in $cmd variable. (It could be anything, even some simple bash oneliner.) If ctrl+C is pressed while running the script, I want it to kill the currently running $cmd but it should…
Miroslav
  • 528
  • 1
  • 6
  • 11
5
votes
2 answers

How do I trap SIGQUIT properly in a bash script?

I can write shell scripts that trap SIGINT just fine, but I can't seem to trap SIGQUIT. #!/bin/bash function die { echo "Dying on signal $1" exit 0 } trap 'die "SIGINT"' SIGINT trap 'die "SIGQUIT"' SIGQUIT while true; do echo…
Maxy-B
  • 2,772
  • 1
  • 25
  • 33
5
votes
1 answer

Unable to trap signals in docker entrypoint script

I have a docker entrypoint script that is supposed to trap signals sent to processes in the container. The main application is tomcat - java process embedded in the docker-entrypoint.sh which is passed to dumb-init. The process mapping in the…
IPlato
  • 191
  • 2
  • 7
5
votes
1 answer

Bash function scope status when EXIT trap is executed after a program exits with status != 0 (set -e)

Declaring a local variable in a bash function makes that variable only visible inside the function itself and its children, so if I run: #!/bin/bash set -e func_one() { echo "${var}" } func_two() { local -r var="var from func_two" …
5
votes
2 answers

Bash Trap: How to Get Line Number of a Subprocess with Non-Zero Status

For the Bash program: 1 #!/bin/bash 2 3 trapinfo() 4 { 5 echo "=== Trap Info: Status=$? LINENO=$@ A=$A" 6 } 7 8 main() 9 { 10 trap 'trapinfo $LINENO -- ${BASH_LINENO[*]}' ERR 11 12 set -e 13 set -E 14 set -o…
Steve Amerige
  • 1,309
  • 1
  • 12
  • 28
5
votes
1 answer

Where does the exit status go after trap/return?

I was playing around with using trap inside a function because of this question, and came up with this secondary question. Given the following code: d() { trap 'return' ERR false echo hi } If I run d, the trap causes the shell to return…
kojiro
  • 74,557
  • 19
  • 143
  • 201
5
votes
1 answer

How to trap on_error in bash globally?

It seems on_error trap in Bash works only in the scope of function it was defined in. For instance running this script #!/bin/bash on_error() { echo 'on_error' } f() { false echo 'function f' } g() { trap on_error ERR echo…
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
4
votes
2 answers

Restore traps without a temp file

Without arguments trap prints the currently set commands for all traps. However, a subshell does not inherit traps, so the canonical example for saving and restoring traps fails in bash: save_traps=$(trap) ... eval "$save_traps" The trap on the…
William Pursell
  • 204,365
  • 48
  • 270
  • 300
4
votes
2 answers

`ERR` trap is not executed when syntax error

According to man bash, set -e Exit immediately if (snip). A trap on ERR, if set, is executed before the shell exits. However, the script below doesn't invoke the ERR trap. trap 'echo ERR; sleep 1' ERR trap 'echo EXIT; sleep 1' EXIT set…
ynn
  • 3,386
  • 2
  • 19
  • 42
4
votes
3 answers

How to get return code in TRAP ... RETURN?

This is my code: function my::return() { exit_code="$1" echo "exit status of last command: ${exit_code}" } function testing(){ trap 'my::return $?' RETURN return 2 } If I run testing I expect the exit_code to be 2 as tyhis is the…
Stefan Thorpe
  • 540
  • 1
  • 4
  • 11
1 2
3
11 12