shell sends a signal to the child process

Processing script contains the child process, suppose you want to kill any child processes, we also need to stop these scripts. trapOrder to complete this operation.

Child process

To &the child process runs, the most background programs running, asynchronous between parent and child process

Has been directly run the child process, then the child process running in the foreground, the synchronization between the parent and child process

sleep 10   #等待10秒,再继续下一操作
sleep 10 & #当前shell不等待,后台子shell等待

trap

Unix system by design doesn’t allow any script/program to trap SIGKILL due to security reasons.

Setting a trap for SIGKILL or SIGSTOP produces undefined results.

trap After three treatments capture mode signals

  1. Implementation of a code to process the signal trap "commands" signal-listortrap 'commands' signal-list
  2. The default operation acceptance signal, i.e. the signal restore default action trap signal-list
  3. Ignore the signal trap "" signal-list

Precautions:

  1. SIGKILL SIGSTOP SIGSEGV SIGCONTCapture not set
  2. After the capture signal signal-list specified and finished executing the first command, if the command is not the shell program is terminated, then, the shell program will continue to execute subsequent commands when the received signal performed by the command, which will easily lead to shell program can not be terminated.
  3. In the trap statement, single and double quotes are different, the program for the first time when the shell hit trap statement will scan commands in the command again. At this time, if the commands are enclosed in single quotation marks, then the shell commands will not be replaced and command variables

Example

All synchronous mode

#!/bin/bash
# trapchild

trap 'echo "[TRAP]I am going down, so killing off my processes.."; ps -a | grep sleep | xargs kill -9; wait; exit' SIGHUP SIGINT SIGQUIT SIGTERM SIGKILL

./a.sh
err=$?
if [ $err != 0 ]; then
    echo 3 $err
    exit 1
fi

echo "[TRAP]my process pid is: $$"
echo "[TRAP]my child pid list is: $pid"

wait
#!/bin/bash
# trapchild

trap 'echo "[a]ash I am going down, so killing off my processes.."; ps -a | grep sleep | xargs kill -9; wait; exit' SIGHUP SIGINT SIGQUIT SIGTERM SIGKILL

sleep 120
echo 1 $?
if [ $? != 0 ]; then
    echo 2 $?
    exit 1
fi

pid="$!"

echo "[a]my process pid is: $$"
echo "[a]my child pid list is: $pid"

wait

Execution trap.shwill process after three

$ ps -a
  PID TTY          TIME CMD
  750 pts/5    00:00:00 trap.sh
  751 pts/5    00:00:00 a.sh
  752 pts/5    00:00:00 sleep

At this point execution kill -15 750or kill-15 751will not work, only the implementation kill -15 752will work, but trapwill not trigger

Using asynchronous mode

#!/bin/bash
# trapchild

trap 'echo "[TRAP]I am going down, so killing off my processes.."; ps -a | grep sleep | xargs kill -9; wait; exit' SIGHUP SIGINT SIGQUIT SIGTERM SIGKILL

./a.sh &
err=$?
if [ $err != 0 ]; then
    echo 3 $err
    exit 1
fi

echo "[TRAP]my process pid is: $$"
echo "[TRAP]my child pid list is: $pid"

wait
#!/bin/bash
# trapchild

trap 'echo "[a]ash I am going down, so killing off my processes.."; ps -a | grep sleep | xargs kill -9; wait; exit' SIGHUP SIGINT SIGQUIT SIGTERM SIGKILL

sleep 120 &
echo 1 $?
if [ $? != 0 ]; then
    echo 2 $?
    exit 1
fi

pid="$!"

echo "[a]my process pid is: $$"
echo "[a]my child pid list is: $pid"

wait

Execution trap.shwill process after three

$ ps -a
  PID TTY          TIME CMD
  750 pts/5    00:00:00 trap.sh
  751 pts/5    00:00:00 a.sh
  752 pts/5    00:00:00 sleep

If it does kill -15 752not trigger the trapexit

$ ./trap.sh
[TRAP]my process pid is: 2163
[TRAP]my child pid list is:
1 0
[a]my process pid is: 2164
[a]my child pid list is: 2165

If you do kill -15 751, triggera.sh trap

$ ./trap.sh
[TRAP]my process pid is: 2324
[TRAP]my child pid list is:
1 0
[a]my process pid is: 2325
[a]my child pid list is: 2326
[a]ash I am going down, so killing off my processes..
kill: failed to parse argument: 'pts/5'

If you do kill -15 750, triggertrap.sh trap

$ ./trap.sh
[TRAP]my process pid is: 2487
[TRAP]my child pid list is:
1 0
[a]my process pid is: 2488
[a]my child pid list is: 2489
[TRAP]I am going down, so killing off my processes..

other

When the script directly synchronous, asynchronous mixed use need to be thoughtful, detailed test

Ref

  1. Shell Programming trap command
  2. Use trap in the script

Original: Large column  shell sends a signal to the child process


Guess you like

Origin www.cnblogs.com/chinatrump/p/11606704.html