标签:ext pid for ash trap命令 完成 tty bin tin
处理包含子进程的脚本,假设您希望终止任意子进程,那么还需要停止这些脚本。trap
命令完成此操作。
以 &
运行的子进程,最为后台程序运行,父进程与子进程之间为异步
已直接方式运行子进程,此时子进程在前台运行,父进程与子进程之间为同步
sleep 10 #等待10秒,再继续下一操作
sleep 10 & #当前shell不等待,后台子shell等待
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
捕捉到信号之后三种处理方式
trap "commands" signal-list
or trap 'commands' signal-list
trap signal-list
trap "" signal-list
注意事项:
SIGKILL
SIGSTOP
SIGSEGV
SIGCONT
不设置捕捉#!/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
执行trap.sh
之后会有三个进程
$ 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
此时执行 kill -15 750
或 kill-15 751
不会起作用,只有执行 kill -15 752
才会起作用,但是 trap
不会触发
#!/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
执行trap.sh
之后会有三个进程
$ 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
如果执行 kill -15 752
,不会触发 trap
直接退出
$ ./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
如果执行 kill -15 751
,触发 a.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'
如果执行 kill -15 750
,触发 trap.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..
当脚本直接同步、异步混合使用时需要考虑周全,详细测试
原文:大专栏 shell向子进程发送信号
标签:ext pid for ash trap命令 完成 tty bin tin
原文地址:https://www.cnblogs.com/petewell/p/11606639.html