Use this tag for questions about the `trap` Bash built-in.
Questions tagged [bash-trap]
171 questions
4
votes
4 answers
How to immediately trap a signal to an interactive Bash shell?
I try to send a signal from one terminal A to another terminal B. Both run an interactive shell.
In terminal B, I trap signal SIGUSR1 like so :
$ trap 'source ~/mycommand' SIGUSR1
Now in terminal A I send a signal like so :
$ kill -SIGUSR1…

ogr
- 610
- 7
- 23
4
votes
1 answer
Bash traps, capture and pass them as arguments on same function
I'm developing a script which manage some traps. At the beginning I only managed INT and SIGTSTP with this code and it works very well:
#!/bin/bash
function capture_traps() {
echo -e "\nDoing something on exit"
exit 1
}
trap capture_traps…

OscarAkaElvis
- 5,384
- 4
- 27
- 51
4
votes
2 answers
Restrict kill commands when running jar file using a shell script
I have a jar file which is a program which accept user input and processes it. I am running this jar file using the below shell script:
PR=`basename $0`
cdt=`date +'%H:%M:%S %d/%m/%Y'`
cd $HOME/myprogram
java -cp…

lang_android
- 171
- 1
- 1
- 10
4
votes
1 answer
BASH break readline on SIGINT
I try to catch a SIGINT ctrl+c inside a read loop on a socket but the break I'm asking for only get processed after a new line. So when I ctrl+c my running app, I have to wait for a new line to arrive in the socket before the loop breaks / the app…

Vincent Duprez
- 3,772
- 8
- 36
- 76
4
votes
1 answer
How to trap exit 1 signal in Shell script?
I would like to try trapping signal 1 but fail
#!/bin/bash
# capture an interrupt # 0
trap 'echo "Exit 0 signal detected..."' 0
trap 'echo "Exit 1 signal detected..."' SIGHUP
# display something
echo "This is a checkpoint 1"
exit 1
echo "This is…

TheOneTeam
- 25,806
- 45
- 116
- 158
4
votes
2 answers
Shell Script get CTRL+Z with Trap
I am trying to get the SIGSTOP CTRL+Z signal in my script's trap.
When my script is executing, if I temporarily suspend from execution, send a SIGSTOP signalCTRL+Z, it needs to remove the files I create in it and to kill the execution.
I don't…

Dragos Rizescu
- 3,380
- 5
- 31
- 42
4
votes
1 answer
Recover after bash trap
I've a bash script with some file manipulations and I would like to process a loop until the end of the block after pressing CTRL+C. I've made an example:
#!/bin/bash
# Register signal handler
ABORT=0;
trap ABORT=1 SIGINT;
# Create temp…

Urs Marti
- 43
- 2
4
votes
1 answer
$? inside bash trap
From a bash script, I'm trying to handle segmentation faults from a c++ program. I've read that using trap on SIGCHLD can be used for this purpose. Inside the trap I should be able to test for $? to get the return code from the program. See…

RaveTheTadpole
- 1,515
- 13
- 25
4
votes
2 answers
bash trap won't ignore signal
Please consider this bash-script:
#!/bin/bash
trap '' INT
echo sleep:
sleep 5
echo rsync:
rsync -a /usr/lib /var/tmp
Trying to interrupt sleep with ctrl-c fails, as expected. But rsync is interruptible (the order of sleep & rsync doesn't…

user1854128
- 41
- 4
4
votes
2 answers
how to get the original caller lineno when executing a function returning a non-zero value
I've made a func.sh script to be sourced containing:
1. function testfunc ()
2. {
3. echo "--> TESTFUNC CALLED"
4. caller 0
5.
6. # here I mimic that something went wrong
7. echo "now I return a non-zero value"
8. return 1
9. }
Then I've…

Luca Borrione
- 16,324
- 8
- 52
- 66
4
votes
2 answers
Bash: How to trap the Error reason?
I want to trap the error inside the shell script, and then generate some report for the reason for the error:
trap 'error_handler' ERR
In my error_handler function, I want to give the reason for why the ERR signal was caught (e.g. "permission…

TheOneTeam
- 25,806
- 45
- 116
- 158
4
votes
1 answer
why is this simple bash trap failing
I'm still pretty new to bash scripting, and I'm having a hard time figuring out why this simple trap is not working as expected.
Goal - create an optional waiting period that can be skipped by pressing CTRL+C.
Expected result of pressing CTRL+C -…

Craig Cummings
- 43
- 1
- 3
4
votes
1 answer
Concurrent logging in bash scripts
I am currently trying to figure out why a shell script fails at concurrent logging every once in a while.
I have a shell function like the following:
log()
{
local l_text=$1
local l_file="/path/to/logs/$(date +%Y%m%d)_script.log"
local…

LiKao
- 10,408
- 6
- 53
- 91
3
votes
2 answers
How to trap on exit 1 only in bash
I have the following bash script:
#!/bin/bash
function hello {
echo "Hello World!"
}
trap hello EXIT
The problem is that this function will execute on any exit code for that script. if I append an exit 1 at the end of the script, Hello…

Simon Ernesto Cardenas Zarate
- 2,636
- 1
- 28
- 42
3
votes
2 answers
Capture the error stream of a line of code and report it with trap while keeping the stdout untouched
I have the following (simplified) code attempts in 2 shell scripts below.
The script calls an R script in which code is run that will generate standard output and error stream depending on what happens.
What I'm trying to achieve is to have both the…

pieterjanvc
- 273
- 2
- 7