Processing script contains the child process, suppose you want to kill any child processes, we also need to stop these scripts. trap
Order 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
- Implementation of a code to process the signal
trap "commands" signal-list
ortrap 'commands' signal-list
- The default operation acceptance signal, i.e. the signal restore default action
trap signal-list
- Ignore the signal
trap "" signal-list
Precautions:
SIGKILL
SIGSTOP
SIGSEGV
SIGCONT
Capture not set- 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.
- 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.sh
will 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 750
or kill-15 751
will not work, only the implementation kill -15 752
will work, but trap
will 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.sh
will 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 752
not trigger the trap
exit
$ ./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
Original: Large column shell sends a signal to the child process