Questions tagged [trap]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
1 answer
40 views

Why does a command with a non-zero exit status send an ERR signal even when it is “part of a && or || list”?

man bash includes this documentation for using trap: trap [-lp] [[arg] sigspec ...] … The ERR trap is not executed if the failed command is part of the command list immediately following a ...
user avatar
0 votes
0 answers
61 views

BASH - Getting stderr message into a variable to be passed to a trap

My question is relative to Ubuntu 20.04 where I am performing these tasks. I currently pass some state information to a debug trap such as $BASH_COMMAND, $LINENO and $?. I would like to pass any ...
user avatar
1 vote
1 answer
60 views

bash script print stderr on failure

I am trying to make the bash script silent if everything goes fine but print out all the stderr and debug info if it crashes for some reason. Below is what I have so far. #!/usr/bin/bash set -e rm -...
user avatar
  • 4,039
0 votes
2 answers
129 views

How do you continue execution after using trap EXIT in bash?

Environment: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin20) I'm attempting to trap the exit from another function but then continue executing the program. In an object oriented language ...
user avatar
1 vote
1 answer
115 views

Setting a trap for INT doesn't work in a subshell

$ bash -c "trap \"echo INT\" INT; sleep 3" & pid=$!; sleep 1; kill -INT $pid; wait [1] 27811 INT [1]+ Done bash -c "trap \"echo INT\" INT; ...
user avatar
  • 2,893
0 votes
0 answers
58 views

EXIT trap is ignored on Ctrl-C when PID == 1

a.sh: trap 'echo cleanup' EXIT sleep 3 To reproduce, press Ctrl-C before the script finishes: $ docker run --rm -itv $PWD:/app -w /app alpine:3.14 sh a.sh ^Ccleanup $ docker run --rm -itv $PWD:/app -...
user avatar
  • 2,893
0 votes
0 answers
54 views

Shell, traps, signals and conditional cleanup

I have a script running qemu-system in background (-daemonize) and have to clean up (kill) running qemu-system once foreground process completes. Same time I have NOT to clean up (kill) running qemu-...
user avatar
  • 228
1 vote
2 answers
305 views

BASH: Change exit status in trap

Can I assign a different exit status to a shell script in a trap handler? Through trial-and-error, I found that calling exit in trap the exit status can be changed. Normal commands, whether fail or ...
user avatar
  • 409
1 vote
1 answer
135 views

trap "ctrl-c" to kill background shell

background bash 4.2 I have two files envar and main.sh envar ... # displays a spinner so that user knows the job is under processing. spinner() { spin='-\|/' i=0 while kill -0 $1 2>/dev/...
user avatar
  • 147
0 votes
0 answers
134 views

Ignore SIGHUP with trap while still being able to receive output and send input

So i already know one can ignore HUP by doing: trap "command" HUP but since that completely background the process, is it possible to do the same but still being able to send input to said ...
user avatar
0 votes
1 answer
141 views

Bash trap wrong $BASH_COMMAND

I created two traps trap function1 DEBUG trap pwd ERR function function1 { echo $BASH_COMMAND } If I use some wrong command let's say a, then output is-> a bash: a: command not found a /home/user/...
user avatar
  • 5,410
0 votes
1 answer
83 views

Bash: adding history -a to PROMPT_COMMAND makes incorrect calculation of last command's wall time

To calculate last command's wall time I use this code (code #1): # # Taken from https://stackoverflow.com/a/34812608/9881330 # Slightly modified. # function timer_now { date +%s%N } function ...
user avatar
  • 355
0 votes
1 answer
201 views

Why do trap sometimes ignore signals?

I have the following script #! /usr/bin/bash trap "{ echo bye ;}" SIGINT while : do echo "Hello" done When I press Ctrl-C the while loop is not stopped and echo ...
user avatar
  • 3,939
2 votes
1 answer
722 views

How to trap EXIT unconditionally (even if interrupted), in Zsh?

I would like to trap the exit hook of a function. Bash provides a nice signal called RETURN (non-POSIX) which get called when a function returns, such as function test_trap() { trap 'echo trapped' ...
user avatar
  • 1,073
0 votes
0 answers
79 views

Gracefully terminate the executing line of a bash script

The manual workflow I want to automate is as follows: $> adb shell screenrecord /sdcard/file.mp4 This starts a recording on a connected Andfroid device and writes it on the device's sd card. This ...
user avatar
0 votes
2 answers
772 views

Is the sigspec for 0 EXIT or SIGEXIT or exit?

I need to write a trap which tracks when a process exits (amongst other things) and removes a temporary file. I'd prefer to use the sigspec instead of number for better code readability, the relevant ...
user avatar
1 vote
1 answer
324 views

Trap SIGPIPE upon writing to FIFO

These are the steps to reproduce the problem. writer: #!/bin/bash trap 'echo NoReader!' PIPE cat > fifo Open two terminals. I will denote them by the >T1< and >T2< headers below, and ...
user avatar
  • 17.3k
4 votes
2 answers
404 views

trapped in bash traps (RETURN trap)

I am looking for an idiomatic pattern to use traps as a meaning "ensure" or "finally", so they should execute no matter how a bash function exits. I've found the RETURN trap, but ...
user avatar
  • 388
2 votes
0 answers
173 views

Trap on DEBUG signal for the ash and dash shells?

With the bash shell, you can install a trap on the synthetic DEBUG signal. This is useful and will execute your function or bit of code on every shell command. Not so in ash or dash. $ trap 'echo hi' ...
user avatar
2 votes
0 answers
48 views

Why does trap not get triggered when sourcing a file that does not exist?

#!/bin/bash set -eEuo pipefail set -x function deploy_cleanup(){ echo "---Cleaning up---" echo "hello" } trap deploy_cleanup ERR whoami source euaueoaue Outputs: + ...
user avatar
1 vote
1 answer
651 views

Termination of ssh with Ctrl-C trap in bash script

When I press Ctrl+C with the first script, it works as I expect, Ctrl+C doesn't have any effect #!/bin/bash trap '' INT ssh user@server 'svn checkout ...' echo "done" But with the second script Ctrl+...
user avatar
  • 13
0 votes
0 answers
27 views

Strange behavior of bash and SIGTERM with Ubuntu [duplicate]

Here's what happens in Ubuntu (Linux ubuntu 5.3.0-53-generic #47~18.04.1-Ubuntu SMP Thu May 7 13:10:50 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux, bash 4.4.20(1)): In Terminal, open a window (let's call ...
user avatar
2 votes
2 answers
210 views

Capture bash function's result and allow it to exit

This function should exit the calling script: crash() { echo error exit 1 } This works as expected: echo before crash echo after # execution never reaches here But this does not: echo ...
user avatar
  • 1,413
2 votes
1 answer
2k views

Proper way to trap in zsh script

I'm having trouble getting a trap function in a zsh shell-script to work without exiting the shell. I have a simple countdown timer that I want to be able to interrupt using Ctrl+C, and when I do I ...
user avatar
1 vote
1 answer
316 views

What is the difference between the single and double quotes used with the trap command?

I'm trying to learn how to use the signal trap command on Bash. The trap command is quite easy to use, but the issue that hangs in my head is the importance of using single and double quotes. What is ...
user avatar
  • 1,072
1 vote
1 answer
840 views

SNMPv3 traps in Zabbix

My question is about zabbix traps with SNMPv3 and snmptrapd service using zabbix_trap_receiver.pl. I have a switch on which I enabled SNMPv3 only, so the switch has no SNMPv1/2c rw or ro communities ...
user avatar
0 votes
2 answers
908 views

bash: experiment to save and restore global variable context

I made a small experiment to save in one line some context. It works exactly as I wanted. So this post is: 1. to share it with the community. 2. to improve it, or have a complete other solution ...
user avatar
  • 461
0 votes
0 answers
169 views

Bash Trap Handler Race Condition

I am playing with bash's trap feature. And the following script exhbits some kind of race condition. #! /bin/bash set -x trap sigusr1 USR1 ANCESTOR_PID=$$ function sigusr1 { num+=1 declare ...
user avatar
  • 101
1 vote
0 answers
2k views

'unbound variable' error when using function-level bash trap (trap ... RETURN)

Consider this script that calls into a function that sets up a trap for the RETURN of that function: #!/usr/bin/env bash set -euo pipefail function f1() { local temp_file temp_file="$(mktemp)" ...
user avatar
1 vote
1 answer
1k views

trap command not found [closed]

I run into this weird behavior of trap. According to trap's manpage: When a subshell is entered, traps that are not being ignored are set to the default actions. This does not imply that the trap ...
user avatar
2 votes
0 answers
569 views

Disabling "some" traps temporarily, and re-enabling them

I need to disable some(not all) of the traps temporarily in my function and then re-activate them. But I get an unexpected behaviour(I'm pretty new to traps and signals though) when I'm doing it: ...
user avatar
  • 471
1 vote
1 answer
433 views

Can a program trap on memory access to certain regions of process memory?

I'm looking for a way to have an event get triggered and trapped when accessing a specific read-only section of a program's memory. Specifically, if I have a section of program memory that contains a ...
user avatar
5 votes
1 answer
615 views

What is the use of declare with option -t

If you call the command help declare. You will see the following information: -t NAME : to make NAMEs have the `trace' attribute Is there any example that demonstrates the use of this option. I ...
user avatar
  • 159
0 votes
1 answer
585 views

Question about debug trap

I have a script as below: function dbgtrap { echo "badvar is $badvar" } badvar=0 function some_func { badvar=1 badvar=2 badvar=3 } set -o functrace trap dbgtrap DEBUG some_func ...
user avatar
  • 159
7 votes
3 answers
2k views

EXIT Trap with POSIX

I have a script I wrote in Bash that I'm trying to modify to be POSIX-compliant. I have managed to get everything working except I cannot get the EXIT trap to trigger when the process is terminated ...
user avatar
  • 71
3 votes
2 answers
3k views

unable to launch any program: trap int3 ip error

I cannot launch any program in my desktop environment. I get these errors in dmesg: traps: terminator[3670] trap int3 ip:374dda71261 sp:388624bbec0 error:0 traps: pcmanfm[3685] trap int3 ip:...
user avatar
4 votes
2 answers
1k views

Bash ignoring SIGINT trap when 'select' loop is running

When i use 'trap' combined with select loop, namely when i try to hit CTRL+C to break out while options are displayed, it will just print ^C in terminal. If i remove 'trap' from script it will ...
user avatar
0 votes
1 answer
1k views

Use bash EXIT trap to confirm or cancel ctrl+d [duplicate]

Is there a simple way to require confirmation before logging out of a shell, and to prevent the shell from exiting if confirmation is denied? This would be useful to avoid accidentally terminating an ...
user avatar
5 votes
1 answer
638 views

How to make `trap` know if the EXIT is after successful program finish or because of premature as an error or something

PROBLEM: I have a shell program that I have been writing but I can't find out how to make sure that trap is trapping for cleanup at the end or because of a error in some command, it cleans up either ...
user avatar
0 votes
0 answers
208 views

trap signal in bash initramfs fails to work

inside /init bash script executed by the kernel in initramfs.img i need to send a signal to drop me to a rescue shell, so i have trap "exec /bin/bash" SIGINT but ctrl+C isn't effective. First why ...
user avatar
  • 323
0 votes
1 answer
164 views

Deleting temporary direction via trap

I have a script which looks like this: #!/bin/bash set -e tmpdir=$(mktemp -d) pushd $tmpdir trap 'popd && rm -rf $tmpdir' EXIT # Business logic which may succeed or fail... /bin/false ...
user avatar
  • 8,619
2 votes
1 answer
202 views

Why does trap ERR behaviour differ over bash versions 3 and 4?

Background Execute the following code in bash 3, 4 and 5 respectively, and you will get differing results. (function handle_error () { echo ERROR; }; trap handle_error ERR; (exit 1)) Imagine that (...
user avatar
  • 121
1 vote
1 answer
295 views

Signal trap from background job without pressing enter

In Bash 5 I ran into a situation where I want to do the following: trap 'echo trapped!' USR1 while true; do kill -SIGUSR1 $$; sleep 1; done & But I have to press enter on keyboard every time the ...
user avatar
  • 311
2 votes
3 answers
1k views

How to signal a trap in "global" bash scope from function?

I want to execute a trap command in "global" scope, but the signal will come from within function. Of course it is possible to declare the variable globally beforehand or use the -g declare ...
user avatar
  • 311
2 votes
0 answers
84 views

How to trigger a command after named pipe closes?

How can I execute some command after the named pipe has been readout in other end. I was trying to use trap. But nothing happens. My script is something like below. #!/bin/sh cleanup() { echo "...
user avatar
  • 179
3 votes
1 answer
2k views

Failing on errors inside a function which is called on the left-hand-side of an && expression

I am working on a Bash script with several functions. I would like to exit the script if any commands return an nonzero exit code, unless in a context where that code is being explicitly handled (such ...
user avatar
  • 1
2 votes
1 answer
1k views

using both trap and exec to redirect output to tee in bash

I have script that reads lines from a file, processes each line. I'd like to be able to do some "post-processing" in a way that even when the script didn't finish reading all lines because I pressed ...
user avatar
  • 123
0 votes
0 answers
631 views

Interrupt signal names don't work over ssh: bad trap [duplicate]

If run over ssh, a script with a trap statement with interrupt signal names (SIGINT, etc) gives an error and the trap does not work when an interrupt (eg. Ctrl+C) is sent. Eg: #!/bin/bash trap '...
user avatar
  • 961
11 votes
3 answers
3k views

Trap 'Ctrl + c' for bash script but not for process open in this script

I tried to have an interactive program in a bash script : my_program And I wish to be able to close it with 'Ctrl + c'. But when I do it my script is closing as well. I know about. trap '' 2 ...
user avatar
  • 1,458
0 votes
1 answer
1k views

How to trace snmp trap sent by client process?

We have a process(should be a client program) on RHEL 7.4 that send snmp traps to a Solaris server that has trap receiver process(should be a server program listening on 162/1691) on other machine(...
user avatar
  • 1,246