Questions tagged [trap]
Sending and Trapping Signals
183
questions
-1
votes
1
answer
216
views
What does "trap: SIGINT: bad trap" mean and how do I fix it?
I moved code to a new virtual server running Ubuntu 22.
I have the following (edited to make names shorter) in my crontab so that it runs myCommand only if it is not already running:
*/1 * * * * ...
0
votes
1
answer
52
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
...
0
votes
1
answer
72
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 ...
1
vote
0
answers
21
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 ...
0
votes
0
answers
58
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 '...
6
votes
2
answers
689
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 ...
5
votes
3
answers
996
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 '...
0
votes
1
answer
928
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?
1
vote
1
answer
2k
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
}
...
0
votes
1
answer
98
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 ...
0
votes
1
answer
132
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,...
0
votes
1
answer
118
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
235
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 ...
0
votes
1
answer
115
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:~$...
0
votes
1
answer
380
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 ...
5
votes
1
answer
655
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 ...
1
vote
2
answers
1k
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 ...
0
votes
2
answers
203
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
959
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
501
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
votes
1
answer
196
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
216
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 ...
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 ...
1
vote
0
answers
493
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 ...
1
vote
1
answer
407
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 -...
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 ...
1
vote
1
answer
886
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; ...
0
votes
0
answers
549
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 -...
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 ...
1
vote
1
answer
1k
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/...
0
votes
0
answers
918
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
582
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/...
0
votes
1
answer
250
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
1
answer
791
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
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' ...
0
votes
0
answers
182
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
3k
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
830
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 ...
6
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 ...
2
votes
0
answers
421
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
196
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
2k
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
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
1k
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 ...
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 ...
1
vote
1
answer
662
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
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 ...
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 ...
0
votes
0
answers
306
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 ...
2
votes
1
answer
3k
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)"
...