Questions tagged [trap]

Sending and Trapping Signals

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

Using shopt extdebug inside .bashenv

I'm using Ubuntu within Windows Subsystem for Linux. With the approach suggested by this answer I can run code when a new non-interactive shell is started: $ cat ~/.bashenv if [[ $- != *i* ]]; then ...
Foo's user avatar
  • 232
0 votes
1 answer
44 views

bash DEBUG trap is not inherited by subshell despite set -T

I'm using: $ bash --version GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu) Given: #!/bin/bash validate() { if [[ "$BASH_COMMAND" == whoami ]]; then echo "whoami ...
Foo's user avatar
  • 232
1 vote
0 answers
20 views

How does Xen validate exception handlers installed by Guest OSes?

I don't understand a part of the paper Xen and the art of Virtualization. On the topic of virtualizing exception handling (section 2.1.2), the paper states that each guest OS can register a table that ...
Matteo's user avatar
  • 73
0 votes
0 answers
41 views

Subshell and signal propagation

I am curious about signal propagation in subshells. I have the following bash script: #!/bin/bash function test_trap() { echo "hello" } trap 'test_trap' SIGUSR2 (#subshell trap '...
Daniel's user avatar
  • 1
6 votes
2 answers
667 views

Why does `trap` passthough zero instead of the signal the process was killed with?

Consider the following: #!/bin/bash trap 'echo $?' INT kill -INT $$ Output: 0 Here I would expect 130 for my system. Of course, if I do a Ctrl + C then I get 130. The same thing happens for any ...
Elliot Killick's user avatar
5 votes
3 answers
710 views

How to cleanup on suspense (ctrl-z) in a Bash script?

I have the following script: suspense_cleanup () { echo "Suspense clean up..." } int_cleanup () { echo "Int clean up..." exit 0 } trap 'suspense_cleanup' SIGTSTP trap '...
Sebastian Carlos's user avatar
0 votes
1 answer
443 views

How to track all signal in 'trap'?

Can I test keyboard shortcuts and some kill commands with a script and know which signal is caught by trap?
Gilles Quénot's user avatar
1 vote
1 answer
1k views

Trapping SIGINT so that the process kills itself

So section 5 from here made sense to me and I wanted to implement it. I have this bash script #!/usr/bin/env bash cp aux.sh aux.sh.bak cleanup() { cp aux.sh.bak aux.sh rm -rf aux.sh.bak } ...
leo's user avatar
  • 113
0 votes
1 answer
82 views

How to use trap with child tmux process?

I am looking to execute a function when a child tmux process is terminated. Consider the following script: #!/bin/bash function foo { echo "foo exit" } trap foo SIGHUP tmux this script ...
Foo's user avatar
  • 232
0 votes
1 answer
109 views

How does a trap affect external programs?

When a subshell is entered, traps that are not being ignored shall be set to the default actions Source: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_28 Apparently,...
finefoot's user avatar
  • 3,080
0 votes
1 answer
91 views

Possible security risk by sudo on bash exit

I recently read about using bash's built-in trap command to execute a command when bash exits, for example trap "notify-send test" EXIT would send a desktop notification as soon as bash ...
Otux's user avatar
  • 1
1 vote
1 answer
198 views

Send and trap SIGTTIN SIGTTOU in foreground process?

From the description of signals (reference), it seems like SIGTTIN and SIGTTOU are sent to a process only if it is in background. Pressing Ctrl-s does stop printing in terminal, if such flow control ...
codepoet's user avatar
  • 586
0 votes
1 answer
102 views

Trapping shell script doesn't work when used over SSH [duplicate]

I am working on client and want to run a script on server which includes a trap. The following minimal example works fine if I connect to server and run it in an interactive SSH session: user@client:~$...
finefoot's user avatar
  • 3,080
0 votes
1 answer
274 views

Bash: is it possible at all to trap SIGTERM (or other) in a subshell?

Consider multiple subshells, each writing its own log file. Consider that one subshell failed, leading to sending (using some mechanism) of SIGTERM to all the subshells. Upon reception of SIGTERM all ...
pmor's user avatar
  • 609
5 votes
1 answer
602 views

How can I make a script echo something when it is paused?

I am pausing a terminal script with SIGSTOP, and I want it to print a message that it is being paused. Since I can't trap the SIGSTOP, I've tried sending a SIGUSR1 before the SIGSTOP and trapping ...
Adam M's user avatar
  • 51
1 vote
2 answers
973 views

Trap Ctrl C in script

I need to figure out how to trap Ctrl C, so that if I want to end the script early, it can re-enable the suspend and hibernation modes. I looked at other discussions of trapping Ctrl C, but found none ...
fixit7's user avatar
  • 141
0 votes
2 answers
181 views

How to design reusable script handling temporary files lifecycle for other scripts

I need to create a reusable ("utility") script handling entire set of operations with temporary files for any of my other ("application") scripts: creation tracking of temporaries ...
wass rubleff's user avatar
3 votes
3 answers
896 views

how to prevent sudo password prompt in bash EXIT trap?

Let us assume bash shell script is using trap to perform a cleanup like this #!/bin/bash cleanup() { sudo umount /mnt } trap cleanup EXIT sudo mount /mnt #do long running stuff like sleep 3600 ...
humanityANDpeace's user avatar
0 votes
1 answer
437 views

How to configure the trap command to make it run after a process ends that is started with nohup?

I'm using a tmpfile and pass it to my_exe in my shell script. I need to remove the tmpfile after my_exe is terminated, so I use trap "rm $tmpfile" exit. I also need to prevent my_exe from ...
ceremcem's user avatar
  • 2,351
2 votes
1 answer
178 views

trap is ignored when dialog is running with custom BASH_XTRACEFD

I have following script #!/bin/bash exec 5> >(logger -t $0) BASH_XTRACEFD="5" set -x trap _reboot INT DIALOG_TITLE="This is fancy dialog title" _reboot() { echo ...
Marko Todoric's user avatar
4 votes
1 answer
191 views

Bash redirection weird behavior

I was messing around with trapping exit codes and redirecting stdout and stderr of a case statement when I ran into an interesting outcome that I am hoping someone can shine some light into. I am ...
AnthonyBB's user avatar
  • 351
1 vote
1 answer
3k 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 ...
Quinn Comendant's user avatar
1 vote
0 answers
418 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
374 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 -...
balki's user avatar
  • 4,407
1 vote
3 answers
3k 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 ...
Yzmir Ramirez's user avatar
1 vote
1 answer
805 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; ...
x-yuri's user avatar
  • 3,373
0 votes
0 answers
434 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 -...
x-yuri's user avatar
  • 3,373
5 votes
2 answers
2k 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 ...
Zoltan K.'s user avatar
  • 493
1 vote
1 answer
923 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/...
Lunartist's user avatar
  • 385
0 votes
0 answers
817 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 ...
Nordine Lotfi's user avatar
0 votes
1 answer
519 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/...
Prvt_Yadav's user avatar
  • 5,872
0 votes
1 answer
236 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 ...
pmor's user avatar
  • 609
0 votes
1 answer
722 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 ...
sebelk's user avatar
  • 4,399
3 votes
1 answer
2k 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' ...
Franklin Yu's user avatar
  • 1,239
0 votes
0 answers
176 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 ...
Nino van Hooff's user avatar
0 votes
2 answers
2k 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 ...
computronium's user avatar
1 vote
1 answer
756 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 ...
Quasímodo's user avatar
  • 18.9k
5 votes
2 answers
2k 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 ...
pihentagy's user avatar
  • 478
2 votes
0 answers
398 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' ...
Ilias Karim's user avatar
2 votes
0 answers
177 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: + ...
Chris Stryczynski's user avatar
1 vote
1 answer
1k 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+...
Sergey's user avatar
  • 13
0 votes
0 answers
28 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 ...
diciotto's user avatar
1 vote
2 answers
932 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 ...
lonix's user avatar
  • 1,733
3 votes
1 answer
5k 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 ...
Кафка's user avatar
1 vote
1 answer
619 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 ...
testter's user avatar
  • 1,410
1 vote
1 answer
2k 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 ...
elekgeek's user avatar
0 votes
2 answers
1k 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 ...
Jacques's user avatar
  • 551
0 votes
0 answers
274 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 ...
Archer's user avatar
  • 111
1 vote
1 answer
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)" ...
ahmet alp balkan's user avatar
2 votes
1 answer
2k 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 ...
Tran Triet's user avatar