Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 Developer Survey.

Questions tagged [trap]

The tag has no usage guidance.

0
votes
1answer
25 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 ...
-1
votes
1answer
63 views

Is the term trap (of Bash) more accurate than the generic “callback” term in programming? [closed]

Please consider: scripttmp=$(mktemp -d) cleanup() { rm -rf "${scripttmp}" } trap cleanup EXIT I understand cleanup is a "call(ed)back" function as it's being called just before exiting from ...
4
votes
1answer
86 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
30 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
18 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
45 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
43 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
2answers
74 views

How can I 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
36 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
53 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
137 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
90 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
1k 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
294 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(...
1
vote
1answer
56 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
148 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
573 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?
4
votes
4answers
375 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
347 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
440 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
77 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
377 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
120 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
58 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 ...
5
votes
2answers
257 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
296 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
539 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
221 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
181 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
248 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
65 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
161 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
98 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
362 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
215 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
280 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 ...
4
votes
2answers
957 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 ...
3
votes
0answers
81 views

DEBUG trap statement in ksh93 not executed after first run if it contains exit or return statements

I was trying to write a simple function that checks if a config file is "sanitized" and safe for sourcing or not. I seemingly got it to work in bash, and in ksh (Version ABIJM 93v- 2014-06-25) in the ...
2
votes
0answers
234 views

trapping signals without interfering with other signals

When trapping a signal in a shell script, the trap commands are not executed until the current command finishes. For example, consider the following shell script: #!/bin/bash trap 'echo "SIGTERM ...
0
votes
1answer
54 views

How can I test that the bash interactive shell handles the signal using the handler that I set up via `trap`?

In a bash interactive shell, when I run a trap command (e.g. trap "echo You hit control-C!" INT) to set up a signal handler, does that set up how the bash interactive shell handles the signal? How ...
2
votes
1answer
472 views

Trap does not catch SIGTERM in child script

I have two scripts. First one is basically wrapper that calls subscript and sets timeout. #!/bin/bash # wrapper_script timeout --signal=SIGTERM 50 main_script.sh And if execution of main_script.sh ...
1
vote
0answers
1k views

Restoring tty correctly with stty

I'm writing a bash script to read some user input (like a password request), and I'd like to capture the result and pass it through its stdout. (I'm working in a GNU/Linux environment, with recent ...
2
votes
1answer
51 views

Output signal in trap which caused the trap being called?

Let's say I have the following trap, which catches some signals, which terminate the script: #!/bin/sh trapterm() { echo "Process (PID: $$) interrupted." >&2 exit 2 } # add trap to ...
2
votes
0answers
41 views

`trap n condition` syntax

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap : trap n [condition...] trap [action condition...] If the first operand is an unsigned decimal integer, the shell ...
0
votes
1answer
99 views

Can I capture trap messages along with $LINE_NO?

I have a code like: trap 'error ${LINENO} ${BASH_COMMAND}' ERR I want to capture error message along with this. Is there any built-in variable for printing error message?
4
votes
0answers
543 views

sometimes a trap-and-continue-a-for-loop on ctrl-c in bash doesn't continue [closed]

I have the following trap defined in my (long) script: #!/usr/bin/env bash function continue_on_ctrl_c() { echo "** CTRL-C trapped **" continue # jump to the next iteration in the ...
1
vote
1answer
241 views

Shell scripts connected with pipe: trap termination of one script

I have two shell scripts, one runs on a server and writes some files to a temporary directory. This directory is then sent as a tar archive to stdout. At the end (or when it gets interrupted), the ...
2
votes
2answers
385 views

Reset STDOUT FD in trap

Script #!/usr/bin/env bash # Exit on error. Append "|| true" if you expect an error. set -o errexit # Exit on error inside any functions or subshells. set -o errtrace # Do not allow use of undefined ...
3
votes
0answers
6k views

How to configure snmptrapd to process incoming traps from a Juniper device?

I have a CentOS server (7.2). I am trying to configure this as a SNMP trap receiver. In my snmptrapd configuration, I am calling a very basic shell script just to identify if the trap was received: [...
5
votes
3answers
2k views

Strange problem with trap and SIGINT

Please explain this: #!/bin/bash # This is scripta.sh ./scriptb.sh & pid=$! echo $pid started sleep 3 while true do kill -SIGINT $pid echo scripta.sh $$ sleep 3 done - #!/bin/bash #...