0

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

#!/bin/bash

LOCK="/tmp/performanceScript.lock"

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 $?

}

secuencia=0

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

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

    rm -f $LOCK
    trap - INT TERM EXIT

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

seeing the debug, the function is executed twice.

^C
++ 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
1

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.