Questions tagged [bash-trap]

Use this tag for questions about the `trap` Bash built-in.

171 questions
1
vote
0 answers

Getting the name of a sourced file stored in an alias

I'm trying to add some error checking to my bashrc libs. They're modular and loaded in a for loop: for rcfile in $BASHRCD/*.bash; do source "$rcfile" done I use a flag to enable debugging (if I'm trying to troubleshoot something). I've rewritten…
nfarrar
  • 2,291
  • 4
  • 25
  • 33
1
vote
1 answer

How can I pass in a $1 into a trap and signal

sigusr1() { echo $1;} trap sigusr1 SIGUSR1 I'm a bash newbie! I have the above trap and signal code in my .bash_profile.To trigger sigusr1, i simply call Kill -SIGUSR1 pid in the terminal. I googled to find out how I can pass in a parameter into…
ytbryan
  • 2,644
  • 31
  • 49
1
vote
2 answers

trap in exported function silently ignored

This Bash script behaves as expected. test_this.sh function run_this() { trap "echo TRAPPED" EXIT false echo $? } run_this It prints 1 TRAPPED However, when I try to export this function, it fails to trap. test_this2.sh function…
ishmael
  • 1,796
  • 3
  • 18
  • 19
1
vote
3 answers

trap fails to catch SIGSEGV

I'm using this script to test trap: #!/bin/bash trap "echo segfault!" SIGSEGV g++ forever.cpp ./a.out And forever.cpp just runs a recursive function: void forever(){ forever(); } int main(){ forever(); } However it gives Segmentation…
xiangxin
  • 409
  • 6
  • 18
1
vote
0 answers

Why does bash caller print different linenumbers in the debug trap

I am writing a custom debugger for bash scripts using the DEBUG trap. I stumbled upon negative line numbers when printing stacktraces using caller. It seems like caller's output is different, when I call it inside a trap method which is used from a…
Hachi
  • 3,237
  • 1
  • 21
  • 29
1
vote
1 answer

Why trap signals do not work in Shell functions?

This works well: $./tailx.sh error.log 10.21.xxx.xxx # /tmp/.log.pipe is removed But /tmp/.log.pipe is not removed when executed like this: $source tailx.sh $tailx error.log 10.21.xxx.xxx # /tmp/.log.pipe is not removed I want to know why and…
刘旭_
  • 56
  • 8
1
vote
2 answers

Sending SIGINT to foreground process works but not background

I have two scripts. script1 spawns script2 and then sends a SIGINT signal to it. However the trap in script2 doesn't seem to work?! script1: #!/bin/bash ./script2 & sleep 1 kill -SIGINT $! sleep 2 script2: #!/bin/bash echo "~~ENTRY" trap 'echo you…
Pithikos
  • 18,827
  • 15
  • 113
  • 136
1
vote
2 answers

How to don't trap a return value of a function

How can I do, in a Bash script, to handle the return value of an internal function (which may return a non-zero value) without to be caught by the trap? For instance, if the perform_test returns 1 the script is ended because the non-zero return…
gudepier
  • 3,362
  • 6
  • 22
  • 26
1
vote
1 answer

Bash ERR trap halting program

This trap on ERR halts the script. Other examples show that a trap can be used to continue program execution, so why does this halt? #!/bin/sh -e trap 'echo error' ERR echo begin false echo end Returns $ ./test.sh begin $
Hamy
  • 20,662
  • 15
  • 74
  • 102
1
vote
2 answers

Linux trap best practices for &&

Using trap may help in writing cleanest bash script. However I would like to know whether a solution exists in order to trap an error in the following case: GNU bash, version 4.2.45 case OK #!/bin/bash trap 'echo OK i see error at line…
chris
  • 83
  • 6
1
vote
1 answer

Exiting a function with trap and its error code

Let's assume I have the following function: #!/usr/bin/env bash f(){ trap 'printf "\nAborting\n"; return 1' SIGINT sleep 10 return 0 } If I run f and wait those 10 seconds and then do $ echo $? > 0 That's expected. But if I run f and…
pfnuesel
  • 14,093
  • 14
  • 58
  • 71
1
vote
1 answer

Trap syntax issue in bash

I intend to use trap to execute some clean up code in case of a failure. I have the following code, but it seems to be have some syntactical issues. #!/bin/bash set -e function handle_error { umount /mnt/chroot losetup -d $LOOP_DEV1…
The Governor
  • 1,152
  • 1
  • 12
  • 28
0
votes
1 answer

Why is the exit code always 0 inside handle_exit and how to distinguish error from success?

I have a bash script where I want to do a pg_dumpall and upload it to S3 and then send an email to the admin if something went wrong with the exact error message and another email in case everything works fine. #!/usr/bin/env bash set -e set -E set…
PirateApp
  • 5,433
  • 4
  • 57
  • 90
0
votes
2 answers

Why may USR1 signals sent from background jobs in a Bash script not be reliably received by the parent shell process waiting for their completion?

I have a Bash script running a bunch of background jobs in parallel. Under certain conditions, before a background job completes, it sends a USR1 signal to the spawning Bash process (say, to inform that some process that was run as a part of the job…
ib.
  • 27,830
  • 11
  • 80
  • 100
0
votes
1 answer

How can we get back in the right place (that we specify) after signal?

I have made my own signal handler, but I need to get back before # I NEED TO JUMP HERE echo -e "Input name of the file" read filename so I could input filename several times. But when I execute signal (Ctrl + C), I go into handler and then in the…
CheeseMan
  • 35
  • 1
  • 5