Questions tagged [trap]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
1answer
22 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 ...
0
votes
1answer
25 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 ...
2
votes
1answer
79 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' ...
0
votes
0answers
36 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 ...
0
votes
2answers
94 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 ...
1
vote
1answer
70 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 ...
3
votes
1answer
96 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 ...
0
votes
0answers
20 views

Are there shells that do not run EXIT trap on signals, if you have defined non-empty traps for them?

Say you have traps trap 'echo exit' EXIT trap 'echo INT' INT If you send the shell an INT signal, is it guaranteed that it runs EXIT trap? Does POSIX shell do so? Are there some examples of shell ...
1
vote
0answers
55 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' ...
2
votes
0answers
31 views

Why does trap not get triggered when a 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: + ...
1
vote
1answer
139 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+...
0
votes
0answers
24 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 ...
0
votes
0answers
71 views

bash idiom for running cleanup trap once

I want a cleanup trap to run when a function foo exits. My requirements are: The cleanup function should run on all exit paths of foo (on successful completion or abortion e.g. via exit 1). The ...
1
vote
2answers
48 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 ...
1
vote
1answer
719 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 ...
0
votes
1answer
133 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 ...
1
vote
1answer
382 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 ...
0
votes
2answers
394 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 ...
0
votes
0answers
105 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 ...
1
vote
0answers
901 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)" ...
1
vote
1answer
471 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 ...
2
votes
0answers
288 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: ...
1
vote
1answer
183 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 ...
3
votes
1answer
443 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 ...
0
votes
1answer
405 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 ...
5
votes
2answers
1k 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 ...
2
votes
2answers
2k 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:...
4
votes
2answers
688 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 ...
0
votes
1answer
796 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 ...
5
votes
1answer
331 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 ...
0
votes
0answers
88 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 ...
0
votes
1answer
105 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 ...
2
votes
1answer
129 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 (...
1
vote
1answer
145 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 ...
2
votes
3answers
848 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 option. ...
2
votes
0answers
53 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 "...
3
votes
1answer
737 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 ...
2
votes
1answer
684 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 ...
0
votes
0answers
439 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 '...
11
votes
3answers
2k 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 ...
0
votes
1answer
914 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(...
2
votes
1answer
362 views

Killing background processes started in nix-shell

I am developing a simple data science environment with Python tools and a database. When I enter the nix-shell, I spin up the database process. I would like to spin it down when exiting the ...
2
votes
1answer
447 views

How to trap INT signal infinitely many times?

I'm piping output of an interactive command (ghci) through sed-based script to add some colors: ghci | colorize.sh where colorize.sh is something like: #!/bin/bash trap '' INT sed '...some pattern.....
3
votes
1answer
3k views

Wait for signal

In a bash script, is there a simple way to wait for a signal, something like: wait -s SIGINT or whatever? Maybe just trap?
4
votes
4answers
839 views

Delay termination of script

I have a script like this (written for /bin/sh on OpenBSD) which first updates a local copy of some CVS repositories using rsync, and then updates the checked-out version of these on my machine. The ...
2
votes
1answer
675 views

Trap and collect script output, “input file is output file” error?

I need to upload the output of the current script, so I added a trap and set -ex, e.g #!/bin/bash exec &> /tmp/error.log trap 'cat /tmp/error.log; curl http://127.0.0.1/error.php?hostname=$(...
2
votes
3answers
2k views

How to prevent one command from triggering ERR trap?

I am using an ERR trap to catch any error in my bash script and output what happened to log. (similar to this question: Trap, ERR, and echoing the error line ) It works as expected. The only problem ...
0
votes
1answer
204 views

Choices for dealing with a signal?

From APUE A process has three choices for dealing with a signal. Ignore the signal. This option isn’t recommended for signals that denote a hardware exception, such as ...
1
vote
1answer
960 views

Why does bash exit immediately when waiting for a command to complete and receives SIGHUP for which a trap has been set?

From Bash manual If Bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes. When Bash is ...
2
votes
1answer
362 views

How to start Vim from a trap and still be able to resume it after suspending it?

I have the following code in my ~/.zshrc: nv() ( if vim --serverlist | grep -q VIM; then if [[ $# -eq 0 ]]; then vim elif [[ $1 == -b ]]; then shift 1 IFS=' ' vim --...