linux shell命令之父进程与子进程

trap是Linux的内建命令,它用于捕捉信号,trap命令可以指定收到某种信号时所
执行的命令,比如,trap可以指定收到由ctrl+c组合键所触发的INT信号时,执行
中断处理命令,trap的格式如下:
trap command sig1 sig2... sigN

vi traploop.sh
#!/bin/bash

#一旦收到INT信号,执行双引号内的echo命令

trap "echo 'You hit CONTROL+C'" INT

while :; do #使用冒号表示永真,无限循环
let count=count+1
echo "This is the $count sleep"

sleep 5
done

./traploop.sh
This is the 1 sleep
This is the 2 sleep
This is the 3 sleep
^CYou hit CONTROL+C
This is the 4 sleep
This is the 5 sleep
^CYou hit CONTROL+C
This is the 6 sleep

vi nokillme.sh
#!/bin/bash

trap "" TERM INT #忽略对TERM和INT两种信号的处理
#如果还要忽略其它信号,将它们添加到INT之后

#无限循环,每次进入循环体都休眠5s
while :; do
sleep 5
done
chmod u+x nokillme.sh
./nokillme.sh &
[1] 46532
kill %1 #试图杀死nokillme.sh进程
jobs #nokillme.sh进程仍在运行
[1]+ Running ./nokillme.sh &
kill -9 %1
[1]+ Killed ./nokillme.sh

jobs

当我们用kill %1命令杀死1号作业,由于kill命令发送的是TERM信号,
nokillme.sh脚本的trap命令忽略了对TERM信号的处理,因此kill %1
命令不能杀死nokillme.sh进程。

kill -9 %1,向任务1发送9号信号(kill),等价于kill -KILL %1

子shell能继父Shell所忽略的信号,但是,不能继成父Shell未忽略的信号。

vi forever.sh
#!/bin/bash

while :; do
sleep 5
done

vi subsig.sh
#!/bin/bash

trap "" QUIT #忽略QUIT信号
trap "echo 'You want to kill me'" TERM #捕捉到TERM信号,打印提示信息

( #将forever.sh脚本作为子Shell,子shell将无限休眠
./forever.sh
)

./subsig.sh & #运行subsing.sh
[1] 46590 #返回父进程的作业

kill -3 1876
bash: kill: (1876) - No such process
kill -3 46590 #向父进程发送3号信号,即QUIT信号

ps -a
PID TTY TIME CMD
24345 pts/19 00:00:00 telnet
44710 pts/17 00:00:00 sudo
44746 pts/17 00:00:00 sudo
44747 pts/17 00:00:00 sort
44748 pts/17 00:00:00 sudo
44749 pts/17 00:00:00 sort
44750 pts/17 00:00:00 sudo
44751 pts/17 00:00:00 sort
44759 pts/17 00:00:00 su
44760 pts/17 00:00:00 bash
45497 pts/17 00:00:00 vi
45498 pts/17 00:00:00 vi
46590 pts/17 00:00:00 subsig.sh #父进程未退出,说明QUIT信号被忽略
46591 pts/17 00:00:00 forever.sh
46599 pts/17 00:00:00 sleep
46600 pts/17 00:00:00 ps

kill -3 46591 #向子进程发送3号信号,即QUIT信号,也未退出,说明QUIT信号也被忽略


ps -a
PID TTY TIME CMD
24345 pts/19 00:00:00 telnet
44710 pts/17 00:00:00 sudo
44746 pts/17 00:00:00 sudo
44747 pts/17 00:00:00 sort
44748 pts/17 00:00:00 sudo
44749 pts/17 00:00:00 sort
44750 pts/17 00:00:00 sudo
44751 pts/17 00:00:00 sort
44759 pts/17 00:00:00 su
44760 pts/17 00:00:00 bash
45497 pts/17 00:00:00 vi
45498 pts/17 00:00:00 vi
46590 pts/17 00:00:00 subsig.sh
46591 pts/17 00:00:00 forever.sh
46611 pts/17 00:00:00 sleep
46612 pts/17 00:00:00 ps

kill 46590 #向父进程发送TERM信号
ps -a
PID TTY TIME CMD
24345 pts/19 00:00:00 telnet
44710 pts/17 00:00:00 sudo
44746 pts/17 00:00:00 sudo
44747 pts/17 00:00:00 sort
44748 pts/17 00:00:00 sudo
44749 pts/17 00:00:00 sort
44750 pts/17 00:00:00 sudo
44751 pts/17 00:00:00 sort
44759 pts/17 00:00:00 su
44760 pts/17 00:00:00 bash
45497 pts/17 00:00:00 vi
45498 pts/17 00:00:00 vi
46590 pts/17 00:00:00 subsig.sh #父进程未被杀掉
46591 pts/17 00:00:00 forever.sh
46619 pts/17 00:00:00 sleep
46620 pts/17 00:00:00 ps
kill 46591 #向子进程发送TERM信号
Terminated #子进程立即终止
You want to kill me #打印出父进程的log
[1]+ Exit 143 ./subsig.sh #父进程终止


ps -a
PID TTY TIME CMD
24345 pts/19 00:00:00 telnet
44710 pts/17 00:00:00 sudo
44746 pts/17 00:00:00 sudo
44747 pts/17 00:00:00 sort
44748 pts/17 00:00:00 sudo
44749 pts/17 00:00:00 sort
44750 pts/17 00:00:00 sudo
44751 pts/17 00:00:00 sort
44759 pts/17 00:00:00 su
44760 pts/17 00:00:00 bash
45497 pts/17 00:00:00 vi
45498 pts/17 00:00:00 vi
46626 pts/17 00:00:00 ps

上一篇:Linux基础随笔(关于限制用户单一文件大小及使用系统自带的mail功能)


下一篇:图像轮廓最大内接矩形的求法