Questions tagged [bash-trap]

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

171 questions
2
votes
1 answer

Trap bash errors from child script

I am calling a bash script (say child.sh) within my bash script (say parent.sh), and I would like have the errors in the script (child.sh) trapped in my parent script (parent.sh). I read through the medium article and the stack exchange post. Based…
Vivek Maran
  • 2,623
  • 5
  • 38
  • 52
2
votes
1 answer

Single statement subshell doesn't seem to inherit ERR trap when using "set -o errtrace"

Consider this minimal example, which I run as an executable. #!/bin/bash set -E trap 'echo "ERR trap triggered"' ERR ( echo "hello" >/dev/null ls /root/ ) Notice the two ERR trap messages in the following output: ls: cannot open directory…
jmrah
  • 5,715
  • 3
  • 30
  • 37
2
votes
3 answers

How to trap input to control the screenshot function?

I want to trap input to control the screenshot function. vim myscreenshot.sh screenshot(){ ffmpeg -video_size 1920x1080 -framerate 25 -f x11grab -i :0.0+0,0 /tmp/out.mp4 } close(){ echo "screenshot is over" exit } trap…
showkey
  • 482
  • 42
  • 140
  • 295
2
votes
1 answer

Bash trap is not invoked in function called from loop

A trap invoked in a function called from inside of loop is not invoked. A trap works as expected if invoked in loop body directly. Same function, if called outside of loop, invokes trap. #!/bin/bash function error_handler()…
Vlado B.
  • 75
  • 4
2
votes
2 answers

How can I print to stdout from within a trap called during eval

I expected the following script to print This is redirected to 'output'. when I press ctrl+c: #!/bin/bash trap_function(){ trap '' EXIT INT TERM echo "This is redirected to 'output'." touch this_will_exist } trap trap_function EXIT INT…
katosh
  • 372
  • 4
  • 12
2
votes
2 answers

How to handle interrupt signal when reading from stdin in bash

I'm playing around with bash read functionality. I like what I have so far as a simple layer on top of my current shell. The read -e does tab-complete and previous commands, and sending EOF with ctrl+d gets me back to my original shell. Here's my…
CGanote
  • 149
  • 1
  • 6
2
votes
1 answer

How to kill all the sub processes of bash script when it exits?

I am using named pipes to communicate between 3 kind of process: a producer, some readers and some writers. Then I created a script that simply it runs all the process, and I would like that when the run.sh process is killed or it terminates all the…
Alberto
  • 2,881
  • 7
  • 35
  • 66
2
votes
1 answer

Bash: Ignore SIGNT (ctrl-c) x amount of times

So, in this program, using BASH, I am trying to ignore or trap CTRL-C multiple times (not infinite) before it takes. at the moment I know how to ignore the command once. But I have no idea how to increment or decrement the amount of times it is…
David Walker
  • 41
  • 1
  • 7
2
votes
1 answer

How to use trap to terminate while loop containing ffmpeg fed by redirect from /dev/null?

I discovered here Loop calls process that takes a long time to complete. How do I break out of it? that piping from find to a while loop that calls an ffmpeg cli [HandBrakeCLI] will not process more than one input file because ffmpeg has a quirk…
Lorccan
  • 793
  • 5
  • 20
2
votes
1 answer

Cleaning up after a ruby script -- trapping signals

My ruby script creates a tempfile and spawns an potentially long-running external process. Neither may continue to exist after the script ends, no matter the way in which the script terminates. I thought the following lines would take care of…
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
2
votes
1 answer

Why does my trap does not work?

I have written below script: #!/bin/bash sleep 15 function_signalr() { date date | awk '{printf "%-15s\n", $2}' } trap "function_signalr" 10 When I start the process by "process &" it runs, the PID is given. I do kill -10 PID, but my trap does…
user3214667
  • 57
  • 1
  • 1
  • 2
2
votes
1 answer

How to print a message and continue after trap'ing a signal in bash?

I'd like to to do this in bash: trap "echo Don\'t do that!" 2 3 which works just fine, except that I want the script to continue. How can I do that? If I leave the command as a blank string, the script continues, but does not print anything. Can I…
Johannes Ernst
  • 3,072
  • 3
  • 42
  • 56
2
votes
2 answers

how to prevent a bash script called via su -c from hanging after SIGINT

I have two scripts. These are simplified. The root-script.sh calls userscript.sh: root-script.sh: #!/bin/bash su - user1 -c "/user1path/user-script.sh" user-script.sh: #!/bin/bash trap 'echo please use x for exit' 2 while x=0; do read -p…
Frank
  • 21
  • 1
  • 3
2
votes
1 answer

Trap command in script works when called from CLI but not when used in a PBS job

I have the following simple bash script: #!/bin/bash set -o pipefail set -o errtrace set -o errexit PROGNAME=$0 trap 'echo "${PROGNAME} recieved signal EXIT" | mailx -s "EXIT" "someone@anywhere.com"' EXIT trap 'echo "${PROGNAME} recieved signal…
Polysics
  • 40
  • 4
2
votes
2 answers

my interactive bash script loop is breaking even if I have a trap INT that should prevent it?

on this simple script #!/bin/bash -i trap 'echo "(ctrl+c was hit)"' INT while true; do echo -n "hit Enter..";read echo "still on loop" done if I hit ctrl+c it will exit the loop if I make it not interactive with 1st line like #!/bin/bash,…
Aquarius Power
  • 3,729
  • 5
  • 32
  • 67