Questions tagged [trap]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
56
votes
10answers
188k views

How to stop the loop bash script in terminal?

For example, #!/bin/bash while : do sl done How to terminate this bash script?
31
votes
6answers
50k views

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 ...
2
votes
1answer
2k views

Prevent SIGINT from interrupting function call and child process(es) within

Consider the following script: #!/bin/bash set -o pipefail set -o history trapper() { func="$1" ; shift for sig ; do trap "$func $sig" "$sig" done } err_handler () { case $2 in ...
68
votes
3answers
53k views

“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 ...
14
votes
3answers
2k views

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} } (...
13
votes
2answers
12k views

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 ...
12
votes
6answers
7k views

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 ...
5
votes
3answers
3k 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 #...
12
votes
3answers
12k views

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 ...
12
votes
1answer
7k views

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 ...
7
votes
2answers
5k views

Getting wrong $LINENO for a trapped function

I am writing a Bash script for myself to learn scripting. At some point, I need to add trap in order to clean unwanted directories and files if script is killed. However, for some reason I do not ...
6
votes
4answers
6k views

How can I handle SIGINT trap with a user prompt in shell script?

I am trying to handle SIGINT/CTRL+C interrupt in such a way that if a user accidentally presses ctrl-c, he is prompted with a message, "Do you wish to quit?(y/n)". If he enters yes, then exit the ...
6
votes
3answers
3k views

Bash: issuing trap twice

If I issue the trap builtin twice [for the same signal], what happens? Is the second command added to the first, or does it replace the first? trap Foo SIGINT ... trap Bar SIGINT ... When SIGINT ...
0
votes
1answer
130 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 ...
29
votes
2answers
16k views

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 ...
11
votes
3answers
2k 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 ...
10
votes
2answers
9k views

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 ...
8
votes
1answer
554 views

Standard subshell vs command-substitution subshell

Please explain these trap outputs: $ line(){ echo -------------; echo $BASHPID; } $ trap 'echo bye' EXIT; trap -p; line; (trap -p; line); echo "$(trap -p; line)" trap -- 'echo bye' EXIT trap -- '' ...
6
votes
1answer
1k views

Intercept “command not found” error in zsh

Is there a way to intercept the "command not found" error in ZSH? I've seen this is possible in bash apparently, but I couldn't find anything about doing it in zsh.
4
votes
1answer
1k views

Prevent SIGINT from reaching child processes

I have a bash script for running a server, which will typically be terminated by the user using Ctrl-C. On exit it runs a cleanup function, which I don't want to be interrupted by a second Ctrl-C. #!/...
2
votes
2answers
3k views

how to trap a suspend a resume from a bash script

I have a bash script (on osx) running continuously, it currently traps and handles a terminate like so trap onshutdown TERM how can I make trap also handle suspend/resume like trap onsuspend ? ...
8
votes
1answer
1k views

Trap Ctrl-C in awk script

I believe Ctrl-C can be trapped in bash scripts. Is it also possible to trap it inside an Awk script in order to handle that event? For example, for aborting processing, but printing the results of ...
5
votes
1answer
710 views

bash: Cancelling the execution of 'trap handler DEBUG'

I can get a function myHandler() to execute before a bash command by doing the following: function myHandler() { ... } trap 'myHandler' DEBUG However, I'd like to be able to proceed with or abort ...
0
votes
2answers
128 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 ...