The tag has no wiki summary.

learn more… | top users | synonyms

2
votes
3answers
64 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
76 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
99 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
165 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
153 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
1answer
154 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
71 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
0answers
54 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
89 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 ...
2
votes
0answers
60 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 ...
2
votes
1answer
286 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
113 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
259 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
103 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 ...
3
votes
0answers
1k 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
115 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
738 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
213 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
351 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
140 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 { ...
5
votes
3answers
2k 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 ...
6
votes
2answers
1k 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
269 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 ...
9
votes
2answers
2k 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 ...
6
votes
3answers
1k 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 ...
12
votes
6answers
22k views

How to stop the loop bash script in terminal?

For example, #!/bin/bash while : do sl done How to terminate this bash script?
7
votes
1answer
3k 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 ...
6
votes
3answers
3k views

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

Why doesn't an orphan process receive signals?

I have two shell scripts: launch_job.sh and sub_job.sh. If launch_job.sh uses the following method to run sub_job.sh: nohup sub_job.sh & sub_job.sh becomes an orphan, i.e. with PPID 1. I ...
6
votes
2answers
5k views

How can I trap a program that returns 139 (segmentation fault) in bash?

I have a bash script that tests some programs and one of the program returns Segmentation fault so I tried to add a trap in the head of my script: trap "echo 'segfault occured!'" SIGSEGV That ...
3
votes
2answers
798 views

Wrapper program that sets signal handler

I would like to have a wrapper program that runs a given command and sets a signal handler so that it gets run when the command receives a specified signal. The question is this: Is there an ...
3
votes
3answers
3k views

Error handling in shell script

I wrote a shell script run_script.sh, which includes a step which creates an empty file run_script.lck. Everytime the shell script was called by the cronjob, it will check for the existence of ...
3
votes
2answers
75 views

Text files with sections in common with a given file

I need to maintain a set of plain text files saved in a directory which all have a section taken from another file. An example follows: Files: /directory/textfile1 (every other file in the directory ...
3
votes
2answers
358 views

Bash: keybinding/trap issue

My .bashrc has the following: # Alt+L lists current directory bind -x "\"\el\":ls -ltrF --color=auto;" # trap commands and echo them to xterm titlebar. trap 'echo -ne "\033]0;$BASH_COMMAND - ...
6
votes
3answers
4k 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 ...
1
vote
2answers
1k 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 ? ...
3
votes
1answer
755 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.