Questions tagged [trap]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
1 answer
23 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,...
  • 2,260
0 votes
1 answer
37 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 ...
1 vote
1 answer
49 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 ...
  • 496
0 votes
1 answer
54 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:~$...
  • 2,260
0 votes
1 answer
56 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 ...
  • 457
5 votes
1 answer
396 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 ...
  • 51
0 votes
0 answers
75 views

bash script: Only stop ffmpeg when pressing ctrl+c, not entire script

The question seems like it should have been already asked here, but all I could find are people, who want to do the opposite than me. I have a bash script, that runs ffmpeg at one point and after ...
  • 1
0 votes
2 answers
69 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 ...
3 votes
3 answers
617 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 ...
0 votes
1 answer
157 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 ...
  • 2,151
2 votes
1 answer
119 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 ...
4 votes
1 answer
136 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 ...
  • 351
1 vote
1 answer
2k 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 ...
1 vote
0 answers
206 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
243 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 -...
  • 4,217
0 votes
2 answers
1k 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 ...
1 vote
1 answer
414 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; ...
  • 3,093
0 votes
0 answers
171 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 -...
  • 3,093
1 vote
2 answers
729 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 ...
  • 433
1 vote
1 answer
590 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/...
  • 363
0 votes
0 answers
409 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 ...
0 votes
1 answer
314 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/...
  • 5,642
0 votes
1 answer
153 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 ...
  • 457
0 votes
1 answer
463 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 ...
  • 4,139
2 votes
1 answer
1k 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' ...
  • 1,177
0 votes
0 answers
154 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
2 answers
1k 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
1 answer
517 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 ...
  • 18.3k
5 votes
2 answers
811 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 ...
  • 448
2 votes
0 answers
272 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
0 answers
90 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: + ...
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+...
  • 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 ...
1 vote
2 answers
502 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,571
2 votes
1 answer
3k 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 ...
1 vote
1 answer
472 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,190
1 vote
1 answer
1k 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
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 ...
  • 481
0 votes
0 answers
224 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 ...
  • 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)" ...
2 votes
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 ...
2 votes
0 answers
699 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: ...
  • 601
1 vote
1 answer
676 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 ...
5 votes
1 answer
727 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 ...
  • 159
0 votes
1 answer
752 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 ...
  • 159
8 votes
3 answers
3k 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 ...
  • 81
3 votes
2 answers
4k 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:...
3 votes
2 answers
2k 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
1 answer
2k 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
2 answers
1k 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 ...