Page History
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#!/usr/bin/env bash trap "wait && exit 143" SIGTERMTERM # 15+128 trap "rc=$? && wait && exit $?" EXIT |
...
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 or without error), then the Agent cannot identify the running child process as its parent process has gone. It is therefore recommended that a trap is added to the shell script. This will be triggered on termination of the script - independently of whether the script terminates normally or with an error. This prevents the script from terminating immediately while child processes are running. Instead, in the event of forced termination, the script will continue due to its trap waiting for child processes and the Agent will execute the kill_task.sh
script. This script identifies the Shell Job script process and kills the running child processes.
Download (upload .json): jduExitTrap.workflow.json
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#!/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 SIGTERMTERM' SIGTERMTERM trap 'JS7Trap SIGINTINT' SIGINTINT # 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" |
...
- Line 3 - 11: implements the
JS7Trap()
function including thewait
command. This either waits for termination of child processes or continues immediately.- The exit code returned from the trap in the event of script termination is reported by the task log and order log.
- However, job execution will be considered to have failed regardless of the exit code value as the Cancel/Kill or Suspend/Kill operation has been performed.
- Line 14-16: define traps calling the
JS7Trap()
function in the event of the following signals being received:EXIT
is a summary for a number of signals that terminate a script, however, this is available for the bash shell only.SIGTERM
TERM
is the termination signal sent by the Agent if the Cancel/Kill or Suspend/Kill operation is invoked.SIGINT
INT
is added in case OS processes external to the JS7 Agent send this signal, which usually corresponds to hitting Ctrl+C in a terminal session.
- Line 15-17: starts background processes.
- Line 21 a script should normally
wait
for child processes. However, if this cannot be guaranteed, for example ifset -e
is used to abort a script in case of error, then the use of a trap is an appropriate measure. - The following sequence of actions is performed:
- The job script listed above does not wait for child processes and therefore terminates triggering the EXIT pseudo-signal. The trap function is executed and waits for child processes to be completed. During this period the task process for the job remains alive.
- If subsequently the Cancel/Kill or Suspend/Kill operation is invoked, then the Agent will send a
SIGTERM
signal which:- interrupts the
wait
command in the currently executedJS7Trap()
function, - triggers execution of the
JS7Trap()
function once more and performs thewait
operation for child processes.
- interrupts the
- Having applied the Grace Timeout the Agent executes the
kill_task.sh
script which sends aSTOP
signal to the task process, kills any child processes and finally sends aSIGKILL
signal to abort the task process. - The crucial point is that the job script does not terminate with child processes running but remains active due to triggering of a trap which allows the Agent to kill any child processes from the process tree. If the task process for the job script terminates with child processes running then the Agent cannot identify the process tree and cannot kill child processes.
...