The tag has no usage guidance.

learn more… | top users | synonyms

3
votes
1answer
22 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 ...
1
vote
1answer
37 views

bash script trap for exit and err and logic for difference

I have a bash script and would like to add in the ability to trap for both EXIT and ERR conditions. The structure is currently trap cleanup EXIT function notify(reason) {} function build {} function ...
4
votes
2answers
57 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} } (...
1
vote
3answers
75 views

Why did my trap not trigger?

Given a script that echo's upon receiving a SIGSTOP or SIGHUP signal: $cat test.sh function clean_up { echo "cleaning up!" } echo 'starting!' trap clean_up SIGSTOP SIGHUP sleep 100 I ran it ...
2
votes
3answers
117 views

Why commenting out trap function allows control-c to work?

After standing up rhel VM and I was tailing a log file (tail -f xyz.log) however I realized that control-c didn't work to exit out of it. I tried control-z which did but that left background job so ...
7
votes
1answer
85 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 -- '' ...
0
votes
2answers
45 views

What are signal traps?

Signals are the way of communication between process but I have some questions What are signal traps? How the traps are related to signals in operating system?
1
vote
1answer
53 views

trap errors in bash scripts

I have several questions I want to understand in regards to error trap. why does the code below execute only 1 trap and not 2 and specifically only the code with "mydir2" and not "mydir1"? why at ...
1
vote
1answer
38 views

trap echo does print not write to console when writing to file

When I terminate "CTRL+C" the following script #!/bin/bash function waa(){ for i in {1..10} do echo "start.sh: sleeping $i" sleep 1s done } trap "echo 'GAGA'" TERM waa >&...
1
vote
0answers
22 views

killing subprocesses and traps

I have the following two scripts which simulate some work: start.sh simply launches 2 (mpi) processes with script mpiproc.sh. start.sh #!/bin/bash function trap_with_arg() { func="$1" ; shift ...
8
votes
1answer
149 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 ...
1
vote
0answers
57 views

Redirect SNMP ports to router for use with openHAB SNMP binding

OpenHAB provides an interesting SNMP binding that would allow me (I think) to get network bandwidth use statistics that may be used in rules. Interesting as the may be, it seems that openHAB cannot ...
0
votes
0answers
56 views

spstack, ptrace stopped a process

My server is busy with sys CPU, so I used pstrace to trace it but no output, so I cancel the command inside putty, and then use pstack to trace it again, but no result return either and I closed the ...
2
votes
1answer
90 views

Xvfb not sending SIGUSR1, breaking xvfb-run

I've been working a knotty problem trying to run headless tests using the xvfb-run script provided with Xvfb. It works on my own Ubuntu images running in VirtualBox, but on the Ubuntu 15.04 AMI that ...
2
votes
2answers
327 views

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 ...
6
votes
4answers
438 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 ...
3
votes
1answer
147 views

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

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

grep causes EXIT trap in bash script [closed]

When running the following script in OSX using bash version 3.2 #!/bin/sh set -e function _trap_exit { echo "this is a triggered trap..." } trap _trap_exit EXIT /usr/sbin/ioreg -w0 -l | grep ...
0
votes
1answer
197 views

Skip 'natural' exit in trap

How are you? My script looks like: trap "$(pwd)/dd-destroy.sh $l-env;$(pwd)/dd-drop.sh $l-db;exit" INT QUIT TERM EXIT ./dd-all.sh $l $l-db $l-env || exit 1 app_ip=$(./dd-status.sh ip $l-env|grep ...
1
vote
2answers
302 views

Cannot rewrite “trap command” for SIGTSTP, SIGTTIN and SIGTTOU

I want to trap the SIGTSTP signal, but I'm not able to. When I call just trap command without arguments, I'm getting this: trap -- '' SIGTSTP trap -- '' SIGTTIN trap -- '' SIGTTOU Obviously, my ...
14
votes
2answers
2k 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 reasonably. 1) set -u does ...
3
votes
1answer
286 views

Reading keypresses in shell using trap and Unix signals

I am having trouble understanding the purpose of trap and the multiple stty invocations in the snippet below. I was hoping someone could give me a rundown of what is happening. getkey() { local ...
4
votes
2answers
1k views

Safely exiting while loops in bash

Say I have a bash script that does: while : do foo done I would like to be able to run this script from the console and be able to exit it at an arbitrary time as long as it happens in between ...
2
votes
0answers
383 views

Catch the kill signal when a process is in S state [interruptible sleep]

This is the process group : (ps -ejH) xxxx xxxx xxxx ? 00:00:00 sshd yyyy yyyy yyyy ? 00:00:00 sshd 8713 8713 8713 pts/0 00:00:00 bash 1234 1234 1234 ? ...
6
votes
2answers
2k 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 ...
0
votes
1answer
61 views

Understanding a signal script

I have the following trap script but the idea about it, is little vague to me. #!/bin/bash for i in {1..64}; do trap "echo 'signal[$$] Caught signal SIG$(kill -l "$i")($i)'" "$i"; done; read -s ...
3
votes
3answers
388 views

Bash: issuing trap twice

I have a very simple question. I can't believe the Bash manpage doesn't explain this already, but anyway... If I issue the trap builtin twice [for the same signal], what happens? Is the second ...
5
votes
1answer
400 views

Why different behaviour of trapping EXIT vs INT

Running bash 4.3 on Kubuntu Trusty 64 bit. Please see the following two files: trapping-int.sh #! /bin/bash trap "echo Exiting" INT cat </dev/urandom >/dev/null echo Hello trapping-exit.sh #...
6
votes
1answer
312 views

Cleanup trap for ssh command on multiple remote hosts

I want to execute an arbitrary command on multiple remote hosts using ssh. These commands are mostly long running commands that monitor server resources and aggregate the output to my local ...
2
votes
1answer
790 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 ...
2
votes
2answers
802 views

Can I trap a clock signal in my bash script?

Could I trap a 5-minute signal inside my script? I imagine something like this, function dosomething { echo "It's been 5 minutes." } trap dosomething SIGNAL-EVERY-5-MINUTES while true do ...
6
votes
2answers
1k 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 ...
3
votes
2answers
188 views

Why is my trap not printing any log message?

What I'm doing is, trap 'rm -f /path/of/file/fileName.running; echo "TRAPPED & READY";' 1 2 9 15 >> trap.log I didn't get anything in log & the file which should have been deleted ...
3
votes
1answer
251 views

Errtrace and local

I am using the following script: #!/bin/bash -Eu trap 'echo Hi' ERR exit_failure() { echo "Hello, World!" return 1 } sub_failure() { res=$(exit_failure) } sub_failure It results in the ...
1
vote
1answer
144 views

trapping signals inside grouping commands

I recently ran into a strange behavior. Trap doesn't get executed if inside grouping command: $ cat ./1.sh #!/usr/bin/env bash set -eu trap 'echo exit' EXIT $ ./1.sh exit $ cat ./2.sh #!/usr/bin/env ...
3
votes
0answers
108 views

Remote script cleaning up

I run a remote script using ssh: local script: local_command ssh -p 222 user@server 'sh -s' < ./remote_run.sh remote script: some_command & sleep 10000 trap 'echo exiting' SIGINT SIGTERM ...
3
votes
2answers
2k views

dialog, trap and SIGTRAP

I've seen a lot of examples using dialog tools into scripts with lines as follows: trap "rm $datafile" 0 1 2 5 15 What I don't understand is the meaning of SIGNAL 5, ok I know is SIGTRAP. But shame ...
0
votes
1answer
370 views

Using trap with ERR: How do I send $?

In bash 4.x If I have: err_handler() { echo You received error $1|mail -s "script error" MAILTO } trap err_handler ERR how do I send the actual stderr output to the function when the trap is ...
2
votes
2answers
618 views

Killing background process in bash script when exiting the script

I have a script that tail a file while displaying a clock in the top right corner. I took the clock part from the internet, and it works ok. The entire script is something like (I simplified): while ...
1
vote
2answers
165 views

Default function arguments (manually emitting bash psuedo-signals)

I'm looking for a solution that will automatically allow me to call a function with specific parameters in a certain situation. Here is the specific scenario: When an error occurs, I can set up my ...
5
votes
0answers
4k views

Proper usage of EXIT & ERR traps in bash scripts [closed]

I'm working on a bash script and as I've been going I've learned about traps, signals, function return codes and other such features I've not previously used. I may be thinking about things ...
2
votes
1answer
304 views

Get bash DEBUG trap to execute only once for and/or lists?

Suppose I set a trap on DEBUG like so: unset PROMPT_COMMAND # don't want $PROMPT_COMMAND triggering DEBUG trap trap 'printf %s\\n "most recent history line: $(history 1)" >&2' DEBUG If I do ...
6
votes
3answers
2k views

How to send snmp trap when syslog is logged?

I need to send snmp trap n number of host whenever any syslog is logged in /var/log/messages. How can I do this, can anybody give me any idea?
3
votes
2answers
527 views

Getting the complete command line that caused an ERR , while trap

How do I make trap to return the command that caused an ERR? $function err_handler() { echo "$0 caused the error"; } $ trap err_handler ERR $ grep -ci "failed" test4 &>/dev/null -bash caused ...
2
votes
0answers
534 views

exit code of grep in trap where the script is launched by upstart service

Recently I encountered a problem in ubuntu upstart and trap in Bash shell. I use ubuntu 12.04. The upstart script(/etc/init/player.conf) is: start on (local-filesystems and runlevel [2345]) stop on ...
2
votes
1answer
192 views

prevent trap from altering underscore variable $_

I have the following code in my bashrc to get the execution time of the last command from http://stackoverflow.com/a/1862762 function timer_start { timer=${timer:-$SECONDS} } function timer_stop { ...
6
votes
3answers
4k views

Preventing propagation of SIGINT to Parent Process

Considering a scenario where a Parent program (could be a C++ program or a Shell Script) executes a Child shell script, when we hit Control+C (or whatever character is configured to be the INTR ...
8
votes
2answers
5k 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 ...
3
votes
1answer
510 views

Confirm before exit the command-prompt

I am trying to have a confirmation message every time I type exit command in the command-prompt. To do this, I have tried to use trap in .bashrc file but it seem like trap is not a solution as it run ...