Sending and Trapping Signals
Questions tagged [trap]
177 questions
91
votes
7 answers
"trap ... INT TERM EXIT" really necessary?
Many examples for trap use trap ... INT TERM EXIT for cleanup tasks. But is it really necessary to list all the three sigspecs?
The manual says:
If a SIGNAL_SPEC is EXIT (0) ARG is executed on exit from the shell.
which I believe applies whether…
musiphil
- 1,601
- 2
- 14
- 15
73
votes
11 answers
How to stop the loop bash script in terminal?
For example,
#!/bin/bash
while :
do
sl
done
How to terminate this bash script?
Yinyanghu
- 843
- 1
- 7
- 5
54
votes
9 answers
Trap, ERR, and echoing the error line
I'm trying to create some error reporting using a Trap to call a function on all errors:
Trap "_func" ERR
Is it possible to get what line the ERR signal was sent from? The shell is bash.
If I do that, I can read and report what command was used and…
Anthony Miller
- 1,217
- 3
- 13
- 15
35
votes
3 answers
Correct behavior of EXIT and ERR traps when using `set -eu`
I'm observing some weird behavior when using set -e (errexit), set -u (nounset) along with ERR and EXIT traps. They seem related, so putting them into one question seems reasonable.
1) set -u does not trigger ERR traps
Code:
#!/bin/bash
trap 'echo…
dvdgsng
- 403
- 1
- 4
- 10
22
votes
4 answers
Is trap inherited by a subshell?
I tried a following script:
#!/bin/bash
trap 'echo "touching a file" && touch $FILE' EXIT
foo1(){
echo "foo1"
}
foo(){
echo "foo"
export FILE=${FILE:-/tmp/file1}
}
(foo1)
foo
The output for the above script was:
[root@usr1…
Bhagyesh Dudhediya
- 704
- 8
- 19
21
votes
1 answer
Why do I get an error message when trying to trap a SIGINT signal?
I am running the script below to automatically download files from a server using lftp. It works except that when it runs I get the error message
trap: SIGINT: bad trap
If I replace SIGINT and SIGTERM with INT and TERM then it does work, but I…
flyingace
- 211
- 1
- 2
- 4
20
votes
4 answers
Keep exit codes when trapping SIGINT and similar?
If I use trap like described e.g. on http://linuxcommand.org/wss0160.php#trap to catch ctrl-c (or similar) and cleanup before exiting then I am changing the exit code returned.
Now this probably won't make difference in the real world (e.g. because…
phk
- 5,893
- 7
- 41
- 70
18
votes
7 answers
Silently start task in background
I know that you can use this to start a process in the background, without getting the notification when the process is put in the background and when it is done like so:
(command &) &> /dev/null
However this removes the option to trap when the…
Tyilo
- 5,891
- 12
- 47
- 61
17
votes
1 answer
wait bash-builtin burns a CPU at 100 percent
Occurs at least on GNU bash version 4.3.42 x86_64 && GNU bash version 4.3.11 x86_64
I use sleep & wait $! instead of a simple sleep for getting an interruptible sleep by a signal (as SIGUSR1).
But it seems that the wait bash-builtin behaves in a…
M89
- 758
- 1
- 5
- 10
16
votes
2 answers
How to trigger error using Trap command
I am using Ubuntu 12.04.2. I am trying to use "trap" command to capture abnormal or error in my shell script but I am also trying to manually trigger "Error" exit.
I have tried exit 1, but it won't trigger "Error" signal.
#!/bin/bash
func()
{
…
forestclown
- 301
- 1
- 2
- 7
16
votes
3 answers
What is signal 0 in a trap command?
I'm following this guide on how to set up passwordless SSH authentication with ssh-agent.
To start up ssh-agent the author recommends the following code in .bash_profile:
SSHAGENT=/usr/bin/ssh-agent
SSHAGENTARGS="-s"
if [ -z "$SSH_AUTH_SOCK" -a -x…
Christoph Wurm
- 5,678
- 5
- 25
- 28
15
votes
1 answer
Trap handling and implicit subshells in shell scripts
Let's say you have a shell script that runs some sort of cleanup code
via an EXIT trap, like this:
#!/bin/bash
mytrap () {
echo "It's a trap!" >&2
}
trap mytrap exit
echo I am at the end of the script.
This will, as expected, print out It's a…
larsks
- 32,449
- 5
- 54
- 70
14
votes
3 answers
How to setup trap in bash functions?
I can setup trap in bash scripts but not bash functions.
For example, source the code below and run f, now try Ctrl+C, it's not been trapped!
How to setup trap in bash functions?
4 trap ctrl_c INT
5
6 ctrl_c () {
7 echo "** Trapped…
Cheng
- 6,531
- 11
- 40
- 42
13
votes
2 answers
exit trap in dash vs ksh and bash
Here's a simple script that sets up a temp dir in the current dir and a trap to delete it on exit.
#filename: script
set -x
trap 'rm -rf "$d"' exit
d=`TMPDIR=$PWD mktemp -d`
"$@"
If I do ksh script sleep 100 or bash script sleep 100 and…
Petr Skocik
- 28,176
- 14
- 81
- 141
13
votes
2 answers
On Ctrl+C, kill the current command but continue executing the script
I have a bash script, wherein I execute a line, sleep for sometime and then tail -f my log file to verify a certain pattern is seen, I press ctrl +c to get out of tail -f and then move to the next line till the bash script finishes execution:
Here…
cog_n1t1v3
- 133
- 1
- 1
- 5