Questions tagged [trap]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
1answer
21 views

Proper way to trap in zsh script

I'm having trouble getting a trap function in a shell-script to work without exiting the shell. I have a simple countdown timer that I want to be able to interrupt using ^C, and when I do I want the ...
0
votes
1answer
38 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
38 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
116 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
32 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
185 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
votes
1answer
157 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
105 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
59 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
266 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
99 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 ...
3
votes
2answers
263 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 ...
0
votes
1answer
917 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:...
2
votes
2answers
280 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
280 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
212 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
44 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
72 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
91 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
83 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
446 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
44 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 "...
2
votes
1answer
335 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
346 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
254 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
635 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
194 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
334 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.....
0
votes
1answer
2k views

wait for kill 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?
3
votes
4answers
614 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
535 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=$(...
0
votes
3answers
990 views

exceptions to 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
121 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
631 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
208 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 --...
0
votes
2answers
80 views

Which signal >(process) receives after main shell exits?

This is a Zshell question, although Bash, if it has >(command) syntax (i.e. process substitution of such kind), can hint a solution too. This really basic code explains all: % fun() { setopt ...
4
votes
2answers
575 views

Trap both INT and ERR, but callback got executed multiple times

I'm trapping both INT and ERR with the following code set -ex -o pipefail dest=$(mktemp -d) cd "$dest" trap "echo; echo Clean up; rm -rf $dest" INT ERR sleep 9999 When I press ^C the clean up ...
0
votes
1answer
736 views

Why script that kill itself using a signal handler produce segmentation fault?

SCRIPT GOAL: the script is invoked as ./script.sh cmd1 cmd2 ... cmdn. It should executes in background all the commands passed as arguments from the command line and check when all of them have ...
0
votes
2answers
750 views

Trap all commands in function

I'm working with a set of scripts with functions treated as readonly. The functions are more than just a list of commands, for example, there can be loops and change directories and even calls to ...
0
votes
1answer
354 views

trap is not catching HUP / SIGHUP [duplicate]

I have these bash function: on_ql_trap(){ echo "foobar" > "$HOME/.quicklock/debug.json" } ql_trap(){ trap on_ql_trap EXIT; trap on_ql_trap 0; trap on_ql_trap SIGHUP; trap on_ql_trap HUP;...
1
vote
1answer
359 views

Trap “break” not working while read command is being called

I have this bash-written hangman game for a school project, and currently I am trying to replace the ctrl-c exit command for a while loop break, so the user goes back from the actual game to the ...
1
vote
1answer
462 views

lock file not removed when trap is inside an “if”

I put my trap inside an if, run the script, and after that second execution, it warns that lock file is held (ok). But when I kill -9 the running PID, the lockfile is not removed. When I move trap ...
1
vote
0answers
80 views

What determines a login shell's default trap handlers?

I have a linux box that when I login with ssh, both the keystroke Ctrl-C and manually sending SIGINT will directly terminates the session. λ> ssh root@rhel74 Last login: Sun Mar 18 21:08:02 2018 ...
0
votes
0answers
250 views

trapping HUP/SIGHUP is not working in bash script

I have this bash script: https://gist.github.com/ORESoftware/06dbb26a48d2ddf48a5a2166834e0836 when I call ql_acquire_lock in a shell, if I can call mkdir on a directory, then I acquire that lock. ...
2
votes
1answer
144 views

How to restrict interrupt signal to just the child process?

I have a bunch of log files and I want to do a tail -f on them in a loop such that when I press Ctrl-C, the current tail -f gets killed and I proceed to the next log file: for log in *.log; do ...
1
vote
1answer
521 views

docker-compose, less and SIGINT

I have a script that boots up a test environment using docker-compose. This script pipes the mixed stdout of many docker containers on stdout through less: # This is part of a larger script with some ...
2
votes
1answer
358 views

Run subshells in parallel and collect exit codes

I have the following, I thought it ran the subshells in parallel, but it actually seems to run them in series, and I cannot figure out why: #!/usr/bin/env bash set -e; set -m # allow for job control ...
0
votes
1answer
487 views

Trap handler not working?

Is sth wrong with the following trap? trap cleanup SIGINT cleanup() { echo "Exiting script ..." } I never see the echo command when hitting Ctrl+c The snippet is at the bottom ...
5
votes
2answers
2k views

RETURN trap in Bash not executing for function

I'm dabbling in traps in Bash again. I've just noticed the RETURN trap doesn't fire up for functions. $ trap 'echo ok' RETURN $ f () { echo ko; } $ f ko $ . x ok $ cat x $ As you can see it goes ...