Versions Compared

Key

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

...

  • By default the OS removes child processes if the parent process is killed. However, this mechanism is not applicable for all situations, depending on the way how child processes have been spawned.
  • In order to more reliably kill child processes the Agent makes use of the kill_task.sh script from its var_<port>/work directory.
    • This script identifies the process tree created by the job script and kills any available child processes.
    • Download: kill_task.sh
  • Though the Agent is platform independent it is evident that retrieval of a process tree does not necessarily use the same command (ps) and options for any Unixes.
    • The Agent therefore allows to specify an individual kill script from a command line option should the built-in kill_task.sh script not be applicable to your Unix platform, see JS7 - Agent Operation.

Use of Exit Traps

The Short Version

You can add the following two traps to your Shell Jobs:

Code Block
languagebash
titleExample for concise use of traps for script termination
linenumberstrue
#!/usr/bin/env bash

trap "wait && exit 143" SIGTERM # 15+128
trap "rc=$? && wait && exit $?" EXIT

For explanations see the long version.

The Long Version

In a situation when a Shell Job script starts a background process and does not wait for termination of the child process but instead completes (with our without error), then the Agent cannot identify the running child process as its parent process is gone. It is therefore recommended to add a trap to the shell script that is triggered on termination of the script - independently from the fact that the script terminates normally or with an error. This prevents the script from terminating immediately with child processes running. Instead in case of forced termination the script continues due to its trap waiting for child processes and the Agent executes the kill_task.sh script that identifies the process of the Shell Job script and kills running child processes.

...

Code Block
languagebash
titleExample for Exit Trap on Script for talkative use of exit traps for script Termination
linenumberstrue
#!/usr/bin/env bash

JS7Trap()
{
    rc=$?
    # wait for completion of child processes or let kill_task.sh clean up child processes
    echo "($(date +%T.%3N)) $(basename $0): JS7Trap for signal $1: waiting for completion of child processes ..."
    wait
    echo "($(date +%T.%3N)) $(basename $0): JS7Trap for signal $1: leaving trap, exit code $rc"
    exit $rc
}

# define trap for script completion
trap 'JS7Trap EXIT' EXIT
trap 'JS7Trap SIGTERM' SIGTERM
trap 'JS7Trap SIGINT' SIGINT

# create three child processes
sleep 100 &
sleep 110 &
sleep 120 &

# this is what the script normally should do:
#   echo "waiting for completion of child processes"
#   wait

echo "script completed"

...