Use this tag for questions about the `trap` Bash built-in.
Questions tagged [bash-trap]
171 questions
11
votes
5 answers
Bash not trapping interrupts during rsync/subshell exec statements
Context:
I have a bash script that contains a subshell and a trap for the EXIT pseudosignal, and it's not properly trapping interrupts during an rsync. Here's an…

Zac B
- 3,796
- 3
- 35
- 52
10
votes
2 answers
cp: command not found
I am trying to copy one file to other directory and getting error message while interrupt is called.
The Script :
#!/bin/bash
PATH=~/MkFile/
exitfn () {
trap SIGINT # Resore signal handling for SIGINT
echo ; echo 'Called…

VIJAY GUPTA
- 109
- 1
- 1
- 3
10
votes
1 answer
how can a sourced file inherit trap DEBUG?
Running the following snippet:
#!/bin/bash
function preexec ()
{
echo -e "\n-->preexec command: $BASH_COMMAND"
}
trap 'preexec' DEBUG
function testfunc ()
{
echo "testfunc called $1"
}
testfunc "main"
source "source.sh"
exit 0
where…

Luca Borrione
- 16,324
- 8
- 52
- 66
9
votes
2 answers
Check if trap is set in Bash
Is there a way to check what traps have been set (in the current session or script) in Bash?
Ideally, I'd like to be able to get a list of the signals that have a trap assigned to them, but if that's not possible I can just check each signal…

Kyle Strand
- 15,941
- 8
- 72
- 167
9
votes
1 answer
How to make RETURN trap in bash preserve the return code?
Below is the simplified scheme of the script I am writing. The program must take parameters in different ways, so there is a fine division to several functions.
The problem is that the chainloading of the return value from deeper functions breaks on…

tijagi
- 1,124
- 1
- 13
- 31
9
votes
2 answers
Propagating exit code to caller in case of a shell error from script having an exit trap
Is it possible to propagate an exit code to the caller in case of a syntax error in a Bash script with an EXIT trap? For example, if I have:
#! /bin/bash
set -eu
trap "echo dying!!" EXIT
echo yeah
echo $UNBOUND_VARIABLE
echo boo
Then, running it…

Eemeli Kantola
- 5,437
- 6
- 35
- 43
9
votes
1 answer
Bash script: can not properly handle SIGTSTP
I have a bash script that mounts and unmounts a device, which performing some read operations in between. Since the device is very slow, the script takes about 15 seconds to complete (the mount taking atleast 5-6 seconds). Since leaving this device…

Ram
- 1,161
- 1
- 11
- 34
8
votes
2 answers
Get function backtrace in bash from trap handler (using caller)
I know that you can use 'caller' to get a backtrace of function calls in bash:
#! /bin/bash
Backtrace () {
echo "Backtrace is:"
i=0
while caller $i
do
i=$((i+1))
done
}
myFunc () {
Backtrace
}
myFunc
Prints:
Backtrace is:
11…

nosatalian
- 6,889
- 4
- 20
- 16
8
votes
4 answers
Set trap in bash for different process with PID known
I need to set a trap for a bash process I'm starting in the background. The background process may run very long and has its PID saved in a specific file.
Now I need to set a trap for that process, so if it terminates, the PID file will be…

user647434
- 81
- 1
- 4
8
votes
2 answers
Does trap work as expected while piping?
Here is minimal code for issue demonstration:
http://pastebin.com/5TXDpSh5
#!/bin/bash
set -e
set -o pipefail
function echoTraps() {
echo "= on start:"
trap -p
trap -- 'echo func-EXIT' EXIT
echo "= after set new:"
trap -p
#…

Maxim Kholyavkin
- 4,463
- 2
- 37
- 82
8
votes
1 answer
Get the exitcode of the shell script in a "trap EXIT"
I want to have a cleanup action in my Bash scripts, like this:
#! /bin/bash
set -eu
trap 'echo "E: failed with exitcode $?" 1>&2' ERR
true
false
Using $? came to mind as a natural choice, but this isn't the case. It always contains 0. Is there any…

Roland Illig
- 40,703
- 10
- 88
- 121
8
votes
1 answer
Prevent a bash script from terminating after handling a SIGINT
I am writing a bash wrapper for an application. This wrapper is responsible for changing the user, running the software and logging its output.
I also want it to propagate the SIGINT signal.
Here is my code so far :
#!/bin/bash
set -e; set…

mistyrouge
- 219
- 1
- 7
7
votes
1 answer
capture pid of terminated background process using trap in bash
I'm writing a bash script that spawns a number of background processes. I'd like to be notified whenever a process terminates; and I would like to know the pid of the terminated process.
I have looked at using wait, such that the pids of each…

jbeard4
- 12,664
- 4
- 57
- 67
7
votes
1 answer
bash restart sub-process using trap SIGCHLD?
I've seen monitoring programs either in scripts that check process status using 'ps' or 'service status(on Linux)' periodically, or in C/C++ that forks and wait on the process...
I wonder if it is possible to use bash with trap and restart the…

X.M.
- 941
- 3
- 14
- 22
7
votes
1 answer
What is the actual signal behind ERR
I've read in several places (including SO) that -e is considered "poor form" and is unreliable for exiting a script on any error. A better way to handle errors seems to be using trap, as such:
trap "echo there was an error; exit 1;" ERR
I can't…

SnakeDoc
- 13,611
- 17
- 65
- 97