Use this tag for questions about the `trap` Bash built-in.
Questions tagged [bash-trap]
171 questions
0
votes
0 answers
How to submit the right errorcode to trap
If some error (>0 and not 194) occurs in my script I want to make an API call and report it. But the function that should be triggered by trap, doesnt get executed when tested with exit 1.
I'd expect the function createIncidentOnErr to get executed.…

marcster1973
- 1
- 1
0
votes
0 answers
Bash howto trap errors from inside function
This is working:
#! /bin/bash
set -o errexit
trap 'echo Error on line $LINENO' ERR
echo "start"
false
Output is:
start
Error on line 6
When same code is executed in a function, the trap is not executed:
#! /bin/bash
set -o errexit
trap 'echo…

Tomas Bartalos
- 1,256
- 12
- 29
0
votes
1 answer
How to trap CTRL+Z in Linux POSIX shell script; possible or not?
I am a Linux shell scripter, I would like to know if it is possible to trap signal for script sleep:
That is Ctrl+z?
I do it at the moment like this and would like to trap that sleep signal too.
# define functions to handle signals
# treat them as…

Vlastimil Burián
- 3,024
- 2
- 31
- 52
0
votes
2 answers
How to clean up temporary files after exec-ing inside entrypoint script?
I am trying to write my own mariadb docker image. I wanted to execute some sql statements just after container starts (After exec mysqld). However I found mysqld --init-file option useful for my case. So my entrypoint script is something like…

SkyRar
- 1,107
- 1
- 20
- 37
0
votes
1 answer
Trap command from C program?
I'd like to run a trap '' 2 command from a C program to prevent ctrl-c when the a.out is run.
#define TRAP "trap '' 2"
int main()
{
system(TRAP);
...
}
I can get it to work from a .sh file that also runs the program but I'd like…

Wizzardzz
- 781
- 1
- 9
- 32
0
votes
1 answer
bash trap is not working for SIGTERM or SIGKILL
I have this:
trap on_ql_trap EXIT;
trap on_ql_trap INT;
trap on_ql_trap TERM;
echo "pid that called trap: $$"
which can probably be turned into shorthand:
trap on_ql_trap EXIT INT TERM;
echo "pid that called trap: $$"
when I kill the…

Alexander Mills
- 90,741
- 139
- 482
- 817
0
votes
0 answers
Bash errexit stack traceback glitch
I greatly benefit from having a stack traceback in my bash scripts. However, there was always the periodic glitch that I just ignored until I took the time today to make a minimal working example:
#!/bin/bash # 1
set -o…

Ron Burk
- 6,058
- 1
- 18
- 20
0
votes
1 answer
Kill All Processes Spawned by NPM During Shell Script
I have a shell script that looks roughly like the following:
#!/bin/bash
# Script variables
NPM="/usr/bin/npm"
# Start several sub-processes in a loop in parallel
for i in {1..4}; do
$NPM run -s long_running_script >>…

Oscar Wahltinez
- 1,155
- 3
- 12
- 24
0
votes
2 answers
Trouble with error handling in my first bash script
OK, so I am a total beginner with bash scripts and I am aware that the question is probably phrased a bit awkwardly, but I'll be as clear as I can!
I have written the following script to create a backup of repositories in a folder. The script is as…

jamessct
- 821
- 3
- 10
- 15
0
votes
2 answers
Sending Ctrl_C over ssh
On many of my servers autofs is hung and a process is trying to access a mount, and while I ssh to the remote host the it hangs there until I press Crtl+C from my keyboard
dew:~ # ssh dew00922
but when I press Crtl+C its goes to the prompt
dew:~ #…

Deepak
- 43
- 8
0
votes
0 answers
error handling in nested bash scripts
I have a set of sh scripts:
Main.sh which calls - parent folder
Sub-Main.sh which calls - child folder
Sub-Sub-Main.sh - grandchild folder
It is an installation procedure on Ubuntu.
I am currently do not have an error handling…

user1902346
- 799
- 1
- 12
- 21
0
votes
0 answers
Why are recursive trap calls not happening?
I have a bash script called unions.sh:
_show_help ()
{
echo "$0 [-t time]"
echo
echo "-CARD [num]"
echo " card number"
echo
exit 0
}
_cleanup ()
{
echo Cleaning Up..
exit 5
}
trap _cleanup 0 1 2 5 6
until [ -z…

praveen kumar
- 9
- 2
0
votes
1 answer
minimum time between signals in bash
I'm trying to make a bash script that launches / stops an access point using hostapd.
We have a corner case where we want to switch to another wifi channel as fast as possible, and as hostapd doesn't have support for channel swapping on the fly, we…

Acampoh
- 589
- 1
- 8
- 23
0
votes
1 answer
pgrep -P $$ gives an inexistent process id
#!/usr/bin/env bash
sleep 3 & # Spawn a child
trap '
pgrep -P $$ # Outputs one PID as expected
PIDS=( $( pgrep -P $$ ) ) # Saves an extra nonexistant PID
echo "PIDS:…

Dude
- 11
- 4
0
votes
0 answers
How can one count the number of characters used in a command just executed in Bash?
Let's say I want to have a line print in such a way that it fills the space remaining in a terminal after an executed command. It could look something like this:
user1@host1:~>ls----------------------------------------------------------------
I…

d3pd
- 7,935
- 24
- 76
- 127