Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagebash
titleExample for a cleanup trap
linenumberstrue
#!/bin/bash

# define trap to forward receipt of the SIGTERM signal to a child process
trap 'kill -TERM $CHILD_PID' SIGTERM

# create shell script for background execution
TRAP_SCRIPT=$HOME/test-trap.sh
cat << 'EOF' > $TRAP_SCRIPT
#!/bin/bash

exitOnSigterm()
{
    exec &> /dev/tty
    local signal="$1"
    echo "($(date +%T.%3N)) $(basename $0): trap received signal \"$signal\", cleaning up..."
    if [[ "$CHILD_PID" != "" && -d /proc/$CHILD_PID ]]; then
        procInfo="$(ps -ef | /bin/grep -e PPID -e $CHILD_PID | /bin/grep -v /bin/grep)"
        echo -e "($(date +%T.%3N)) $(basename $0): trap found child process $CHILD_PID:\n$procInfo\n"
        cleanupTemporaryFiles "trap" "$CHILD_PID"
        # traps are not required to kill child processes: the Agent kills child processes
        # /bin/kill -s TERM "$CHILD_PID"
    fi
}

cleanupTemporaryFiles()
{
    exec &> /dev/tty
    tempFile="/tmp/temporary_file.$2"
    if [ -f "$tempFile" ]; then
        rm -f $tempFile
        echo "($(date +%T.%3N)) $(basename $0): cleanup performed by $1 for temporary file: $tempFile"
    fi
}

# add trap to call function on receipt of SIGTERM signal
trap 'exitOnSigterm "SIGTERM"' SIGTERM

# run sleep command in background and create a temporary file for later removal
sleep 120 &
CHILD_PID="$!"
touch /tmp/temporary_file.$CHILD_PID

# wait for completion of shell script or for execution of trap
wait "$CHILD_PID"

# cleanup is performed by trap or by shell script
cleanupTemporaryFiles "shell script" "$CHILD_PID"

exit
EOF

# run shell script in background
chmod +x $TRAP_SCRIPT
$TRAP_SCRIPT &

# wait for completion of shell script or for execution of trap
CHILD_PID="$!"
echo "waiting for completion of child process with pid $CHILD_PID"
wait "$CHILD_PID"

# echo "one more sleep"
# sleep 120

exit $?


Explanation:

  • Line 6 - 50: a sample shell script $HOME/test-trap.sh is created by the job shell script, that is later on started for background execution with line 54.
    • Line 11 - 23: the exitOnSigterm() function is defined that is called should the trap be triggered, see line 36.
    • Line 25 - 33: the cleanupTermporaryFiles() function is defined that removes a temporary file that has previously been created by the job shell script.
      • This function is called by the exitOnSigterm() function of the trap with line 19. The intention is to perform some cleanup in case that a SIGTERM signal triggers the trap.
      • This function is called in line 47. This is intended for normal termination of the job shell script when no trap is triggered.
    • Line 36: a trap is defined that is triggered by a SIGTERM signal and that calls the exitOnSigterm() function.
    • Line 39 - 41: the sample shell script starts a sleep command that is executed in background and touches a temporary file that should be removed by the cleanupTermporaryFiles() function.
    • Line 44: the sample shell script waits for termination of the sleep command.
    • Line 47: the cleanupTermporaryFiles() function is called to remove temporary files in case of normal termination without the trap being triggered.
  • Line 53 - 54: the sample shell script is made executable and is started in background.
  • Line 59: the job shell scripts waits for termination of the sample shell script.Line x - x: the