Questions tagged [bash-trap]

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

171 questions
1
vote
2 answers

BASH Trap CTRL+C Then Exit Script Completely

I've add a trap to my bash script so when CTRL+C is pressed a message appears Do you want to quit ? (y/n) This works at most parts of the script, but fails at others. I've created a simple example that shows it always failing. #!/bin/bash quit()…
Tom
  • 1,436
  • 24
  • 50
1
vote
0 answers

Adding traps to test script running in Travis CI

I have a test script that gets called in .travis.yml: script: - ./test.sh This test script is run whenever a commit is pushed to a repository. This is a bash script in which I create a temporary directory, add a trap to remove that directory when…
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
1
vote
0 answers

ctrl+c doesn't wait for child process (background process) to finish with trap

I have a script which registers a SIGINT trap and starts a ffmpeg background process that records part of the screen. The SIGINT trap sends a SIGINT signal to the background ffmpeg process to get it to gracefully stop and finish the recording. When…
phisch
  • 11
  • 2
1
vote
0 answers

Bash trap does not run when the exit 1 codes put at some random location

I am using the bash trap to make sure one function runs at any cost. I know trap is not specific to exitO or 1. Here is what I have done. #!/bin/bash set -e #array to store server and deployed status declare -A server_deployed #path to file…
Tara Prasad Gurung
  • 3,422
  • 6
  • 38
  • 76
1
vote
2 answers

Parent trap visible but not run by subshell

Tested for Bash 5.0.2 According to the GNU Bash Reference Manual, Bash performs the expansion [of a command substitution] by executing [the] command in a subshell environment According to The Open Group Base Specifications Issue 6: when a…
Marcus Rossel
  • 3,196
  • 1
  • 26
  • 41
1
vote
1 answer

bash cleanup with RETURN trap and resetting handler immediately

Is there a way to disable a trap within the trap handler? I'd like to simplify some code by using the RETURN trap. my_func will return the value of my_command. The tmpfile will be cleaned up as my_func returns. This technique would allow me to…
Raster
  • 157
  • 1
  • 2
  • 7
1
vote
1 answer

Why is trap command not seeing exit code?

Im having issues where the EXIT Trap command is not seeing my exit code. Ive tried just setting a $var from 0 to 1 and right now I'm trying to override the exit with a 1 and base on that having the trap command run certain code. #!/bin/bash if [[ 0…
Tags
  • 172
  • 3
  • 16
1
vote
1 answer

Trap not activated when calling functions

I am using set -e and a trap handler to produce error messages is my ksh scripts. #!/bin/ksh set -e myexit() { if [[ $1 != 0 ]]; then echo "ERROR: Script $0 failed unexpectedly with signal $1!" fi } settrap() { for sig in…
Beginner
  • 5,277
  • 6
  • 34
  • 71
1
vote
1 answer

If you `source` another file, `trap INT` defined before won't work?

If I place this on the top the Bash script, Control+C doesn't work. exit-function() { echo "Hey hey!" } trap exit-function INT But if I put it few lines after, then Control+C is trapped. UPDATE: If it's placed after source…
Cheng
  • 4,816
  • 4
  • 41
  • 44
1
vote
1 answer

Can an approach be found to do set -e/ERR subshell trapping immune to && and || restrictions?

While the Bash man page states that: The ERR trap is not executed if the failed command is ... part of a command executed in a && or || list ... I hoped that code in a subshell would be in a different context and would not be subject to the above…
Steve Amerige
  • 1,309
  • 1
  • 12
  • 28
1
vote
2 answers

Mute the trap in a shell script

I have a shell script which I am using to call many python scripts. I've added a trap in my shell script to catch ctrl+c and exit. But if a python script is running and I hit ctrl+c, it also shows the block of the python script that was being…
1
vote
2 answers

bash trap '' vs trap function passing signals

I'm confused about forwarding signals to child processes with traps. Say I have two scripts: a.sh #!/bin/bash # print the process id echo $$ cleanup() { rv=$? echo "cleaning up $rv" exit } sleep 5 trap '' SIGTERM # trap cleanup…
1
vote
1 answer

How to set bash trap again in trap code?

I have a bash function that is called must be called by an EXIT trap after the first time that it is called. The function sets the trap again to fire as soon as the function exits. echo 0 > .i function launchNextExperiment { ( # Run in nested…
Niel de Wet
  • 7,806
  • 9
  • 63
  • 100
1
vote
1 answer

Bash scripting, trap function not working

I wrote a shell script which runs large simulations and stores things into a temporary file. I used the trap command clean up after receiving a SIGINT. The problem is, the cleanup is not happening. I ran with -x option to debug and the rm command…
mrQWERTY
  • 4,039
  • 13
  • 43
  • 91
1
vote
3 answers

Recursive bash trap to call a function

I have created a bash trap that traps CTRL + C and calls a function ctrl_c. This function just displays a message and starts a counter then returns to the main function. The trap works fine the first time of running but if tried a second time it…
Seatter
  • 408
  • 1
  • 9
  • 19