Questions tagged [bash-trap]

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

171 questions
100
votes
21 answers

Multiple Bash traps for the same signal

When I use the trap command in Bash, the previous trap for the given signal is replaced. Is there a way of making more than one trap fire for the same signal?
jes5199
  • 18,324
  • 12
  • 36
  • 40
68
votes
4 answers

How to trap exit code in Bash script

There're many exit points in my bash code. I need to do some clean up work on exit, so I used trap to add a callback for exit like this: trap "mycleanup" EXIT The problem is there're different exit codes, I need to do corresponding cleanup works.…
Dagang
  • 24,586
  • 26
  • 88
  • 133
40
votes
2 answers

Is it possible to detect *which* trap signal in bash?

Possible Duplicate: Identifying received signal name in bash shell script When using something like trap func_trap INT TERM EXIT with: func_trap () { ...some commands... } Is there a way in the function block to detect which trap has called…
Wolf
  • 513
  • 1
  • 4
  • 7
27
votes
2 answers

How can I achieve bash EXIT trap when exec-ing another binary?

I'd like to use a bash EXIT trap and use exec to avoid spawning a new process. Is this possible? That is, #!/bin/bash touch $0.$$ trap "rm -v $0.$$" EXIT /bin/echo Hello removes the temporary file $0.$$ using bash's EXIT trap…
Rhys Ulerich
  • 1,242
  • 1
  • 12
  • 28
25
votes
6 answers

How can I trap errors and interrupts in GNU make?

I'm wondering if there's a way to implement trap in GNU make, similar to that built into BASH? If the user presses CTRL-C, or if make itself fails (non-zero exit), I'd like to call a particular target or macro.
khosrow
  • 8,799
  • 4
  • 21
  • 24
21
votes
4 answers

How to propagate a signal through a collection of scripts?

I have an collection of scripts which are controlled by a main one. I want to trap the signal ctrl+c in the main script and propagate it to the others. The other scripts should trap this signal as well ( from the main script ) and do some clean-up…
Debugger
  • 9,130
  • 17
  • 45
  • 53
20
votes
1 answer

Save and restore trap state? Easy way to manage multiple handlers for traps?

What is a good way to override bash trap handlers that don't permanently trample existing ones that may or may not already be set? What about dynamically managing arbitrary chains of trap routines? Is there a way to save the current state of the…
Iron Savior
  • 4,238
  • 3
  • 25
  • 30
18
votes
3 answers

Trap function by passing arguments?

I've been searching everywhere and I've come to believe that there is no way to do that other than having global variables but I believe the guru's in stackoverflow.com may be able to help me: Is there any way in bash to trap a function by passing…
Kounavi
  • 1,090
  • 1
  • 12
  • 24
16
votes
2 answers

Unable to trap SIGINT signal in a background shell

I am unable to trap a signal when running in a child / background process. Here is my simple bash script: #!/bin/bash echo "in child" trap "got_signal" SIGINT function got_signal { echo "trapped" exit 0 } while [ true ]; do sleep…
Matthieu
  • 16,103
  • 10
  • 59
  • 86
15
votes
2 answers

How to send a signal SIGINT from script to script?

I want to trap a signal send from Script-A.sh to Script-B.sh so in Script-A.sh i use the command: (Send SIGINT to Script-B.sh) kill -2 $PID_Script-B.sh And in Script-B.sh i catch the signal and call function Clean trap 'Clean' 2 It does…
Debugger
  • 9,130
  • 17
  • 45
  • 53
14
votes
3 answers

When is a signal handled and why does some info freeze up?

Open a terminal named "termA", and run the created file callback.sh with /bin/bash callback.sh. cat callback.sh #!/bin/bash myCallback() { echo "callback function called at $(date)" } trap myCallback SIGUSR1 sleep 20 Open a new terminal…
user7988893
14
votes
2 answers

bash: Why can't I set a trap for SIGINT in a background shell?

Here's a simple program that registers two trap handlers and then displays them with trap -p. Then it does the same thing, but in a child background process. Why does the background process ignore the SIGINT trap? #!/bin/bash echo "Traps on…
Stuart Berg
  • 17,026
  • 12
  • 67
  • 99
13
votes
1 answer

Bash signal capture not detecting variables changed after declaration of 'trap' block

I have a bunch of generic cleanup code that needs to be done whenever a certain bash script exits, whether it exited normally or was interrupted. I figured I'd use the trap "..." EXIT pseudosignal to achieve this. In addition to the generic cleanup…
Zac B
  • 3,796
  • 3
  • 35
  • 52
13
votes
5 answers

Externally disabling signals for a Linux program

On Linux, is it possible to somehow disable signaling for programs externally... that is, without modifying their source code? Context: I'm calling a C (and also a Java) program from within a bash script on Linux. I don't want any interruptions for…
Harry
  • 3,684
  • 6
  • 39
  • 48
13
votes
1 answer

Exit code of traps in Bash

This is myscript.sh: #!/bin/bash function mytrap { echo "Trapped!" } trap mytrap EXIT exit 3 And when I run it: > ./myscript.sh echo $? 3 Why is the exit code of the script the exit code with the trap the same as without it? Usually, a…
blueFast
  • 41,341
  • 63
  • 198
  • 344
1
2 3
11 12