The script has traps that execute a function that reruns the host script, but executes it twice.



exec_func () {
#Creando Script de Vida Eterna.

rm -f $LOCK
cd $( dirname '${BASH_SOURCE[0]}' )
echo "Volvio a la vida" >> /tmp/secuancia_performanceScript.log
$( which bash ) performanceScript2.sh &
exit $?



if (set -o noclobber; echo $$ > "$LOCK") 2> /dev/null;
    trap 'exec_func' INT TERM EXIT SIGKILL

    while true; do
     echo "El numero es $secuencia" >> /tmp/secuancia_performanceScript.log
     sleep 1

    rm -f $LOCK
    trap - INT TERM EXIT

    echo "ya se está ejecutando: $(cat $LOCK)"

seeing the debug, the function is executed twice.

++ exec_func
++ rm -f /tmp/performanceScript.lock
+++ dirname '${BASH_SOURCE[0]}'
++ cd .
++ echo 'Volvio a la vida'
++ exit 0
+ exec_func
+ rm -f /tmp/performanceScript.lock
+++ which bash
++ dirname '${BASH_SOURCE[0]}'
++ /bin/bash performanceScript2.sh
+ cd .
+ echo 'Volvio a la vida'
+ exit 0
++ which bash
+ /bin/bash performanceScript2.sh
ya se está ejecutando: 13963

Why the exec_func function is executed twice?

  • Why don't you produce a minimal script that will still exhibit the same problem? It makes it easier to help you, and in doing that you may discover the problem yourself. – RalfFriedl Aug 13 '18 at 22:34

You are setting a trap on all this signals:

trap 'exec_func' INT TERM EXIT SIGKILL

which makes that the exec_func on INT (ctrl-C) and again on EXIT (end of script).

Choose only one (if that is what you need) like INT

trap 'exec_func' INT

Or identify which signal is being trapped similar to this and use a case statement to decide what to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.