Use this tag for questions about the `trap` Bash built-in.
Questions tagged [bash-trap]
171 questions
1
vote
1 answer
Run a cleanup script when stopping a Docker container
My Dockerfile looks like this:
FROM ubuntu:latest
# ... there is more
ENTRYPOINT ["/root/startup.sh"]
CMD ["choose_random"]
So startup.sh is called with a default paramater if none is provided by the user.
My startup.sh is this one:
#!/bin/bash
#…

Standard
- 1,450
- 17
- 35
1
vote
1 answer
Why is trap in bash throwing an error when empty variable is assigned
I have been using the trap function for a while now, but am running into an issue I don't understand.
Here is a reprex:
err_report() {
echo -e "ERROR LINE $1"
exit 1;
}
trap 'err_report ${LINENO}' ERR
existing=$(echo test | grep -oP…

pieterjanvc
- 273
- 2
- 7
1
vote
1 answer
Bash: Get message from error that was just trapped
Bash allows you to trap signals. Is there a way to actually get the message that printed immediately before (the cause of) a particular signal? It's ERR in particular I'm interested in. I'm aware that not all signals are associated with a message.…

vastlysuperiorman
- 1,694
- 19
- 27
1
vote
0 answers
Trap signal SIGXFSZ not working when using ulimit -f
Trying to trap signal SIGXFSZ when applying ulimit -f in script
my attempts:
script:
#! /bin/bash
function catch_SIGXFSZ {
echo "caught!!"
}
echo running
ulimit -f 1
trap catch_SIGXFSZ 25
cat bigtmp.log > tmp.log
echo after
after running i…

Noam perel
- 11
- 1
1
vote
0 answers
bash trap ctrl c and continue to next function
#!/usr/bin/env bash
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
function ctrl_c() {
echo
echo "** Trapped CTRL-C: Skipping Current Task **"
echo
}
function ping1 () {
sudo ping 8.8.4.4
sudo ping 1.1.1.1
}
function ping2…

Anubhav
- 11
- 2
1
vote
1 answer
How can we prevent CTRL-C from screen terminating?
I'm currently writing a bash script that would create multiple shell instances (with screen command) and execute a subprogram.
The problem is when I try to interrupt the subprogram, it interrupts the screen instance too. I already searched for trap…

Phindau
- 23
- 5
1
vote
0 answers
In a bash read loop, processing ! (exclamation) and trapping CTRL-Z
I am trying to extend bash with custom code that reuses bash's capability to terminate and suspend forked processes and also bash's expansion of !.
I tried a variation of the code in BASH - using trap ctrl+c that has certain lines commented out from…

Prasun Dewan
- 11
- 2
1
vote
1 answer
Is it possible to use Bash history expansion inside the trap command?
Is it possible to use history expansion within a trap [commands] ERR?
I've tried manually enabling history expansion within the environment and within the trap commands argument (trap 'set -H; echo !-2 failed' ERR) but could not get it to work as…

Davi
- 11
- 2
- 3
1
vote
1 answer
Make 'trap ERR' working inside bash functions with 'return' (or in any subshells)
I'm trying to use trap ERR in my scripts. But:
function hmmm() {
trap 'exit 10' ERR
echo 12>/SOME/NONEXISTING/FILE
# some commands that must not be done if previous has failed
echo "THAT MUST NOT BE PRINTED" >&2
return 5
} …

Malamut
- 39
- 3
1
vote
1 answer
Bash Trap SIGINT Multiple Times Doesn't Work
I don't know what's wrong but this script doesn't work.
It should be always goes back to menu everytime I press CTRL+C.
#!/bin/bash
func_info()
{
clear
echo "This is info page."
read -p "Press CTRL+C to back to menu or press enter…

Rebelion
- 13
- 3
1
vote
1 answer
Unable to trup external signal in bash
I'm trying to write a script that will mostly stay idle waiting for a predefined amount of time using regular system sleep. The problem emerges when I try to kill it externally (with start-stop-daemon for example). The main process will be killed,…

e-pirate
- 153
- 1
- 10
1
vote
0 answers
AWS s3 sync keeps failing on SIGTERM trap
I run my script with timeout command which sends a SIGTERM after reaching the timeout limit.
This is for testing when s3 sync command starts, hence the 30s timeout.
My goal is for aws s3 sync command to gracefully finish current upload, and only…

alexfvolk
- 1,810
- 4
- 20
- 40
1
vote
1 answer
How to trap and exit all child processes
I am trying to get hollywood to run in a way that I can exit it with a normal Ctrl+C signal.
Currently I have to press Ctrl+C a bunch of times just to get stuck in the tmux instance that hollywoodcreated. Looking at the source code, there is a trap…

Forivin
- 14,780
- 27
- 106
- 199
1
vote
1 answer
Killing multiple processes in bash upon a single ctrl+c input
My script executes three processes that run cyclically until they are interrupted. I run the first two in background and the last one in foreground.
#!/bin/bash
./p1 &
PID1=$!
./p2 | tee p2.log &
PID2=$!
sleep 1
./p3 "$(head -1 p2.log)" | tee…

Antonio
- 39
- 8
1
vote
1 answer
bash SIGINT trap fires once but never again
I need help understanding exactly how SIGINT is propagated between a shell script's main flow and a function call inside that script. For my script, I have a main menu which takes user input and calls a child function based on that input. The child…

CGanote
- 149
- 1
- 6