当前位置: 技术问答>linux和unix
信号处理问题
来源: 互联网 发布时间:2016-07-07
本文导语: /* 功能只是设置个定时器,用来杀死脚本的子进程,但不知为何却杀掉了脚本进程。。。 调试信息如下: + echo 'in main start $!:' in main start $!: + trap AlarmHandler 14 + SetTimer + DEF_TIME=10 + '[' 10 -ne 0 ']' + echo 'in settimer1 ...
/*
功能只是设置个定时器,用来杀死脚本的子进程,但不知为何却杀掉了脚本进程。。。
调试信息如下:
+ echo 'in main start $!:'
in main start $!:
+ trap AlarmHandler 14
+ SetTimer
+ DEF_TIME=10
+ '[' 10 -ne 0 ']'
+ echo 'in settimer1 $$:3725 $!:'
in settimer1 $$:3725 $!:
+ sleep 10
+ CHPROCIDS=' 3726'
+ TIMERPROC=3726
+ echo 'in settimer2 $$:3725 $!:3726'
in settimer2 $$:3725 $!:3726
+ CHPROCIDS=' 3726 3726'
+ echo 'in main end $!:3726'
in main end $!:3726
+ wait 3726
+ kill -s 14 3725 #不知道是不是这里杀掉了脚本进程,但是我对sigalarm信号设置了自己的处理器,就不应该会杀掉脚本进程
++ AlarmHandler
++ echo 'Got SIGALARMn$$:3725 $!:3726 '
Got SIGALARMn$$:3725 $!:3726
++ KillSubProcs
++ echo 'in killsubprocs CHPROCIDS: 3726 3726 $!:3726'
in killsubprocs CHPROCIDS: 3726 3726 $!:3726
++ kill 3726 3726
alarm_signal.bsh: line 12: kill: (3726) - 没有那个进程
alarm_signal.bsh: line 12: kill: (3726) - 没有那个进程
++ '[' 1 -eq 0 ']'
++ exit 14
*/
#! /bin/bash
AlarmHandler(){
echo "Got SIGALARMn$$:$$ $!:$! "
KillSubProcs
exit 14
}
KillSubProcs(){
echo "in killsubprocs CHPROCIDS: $CHPROCIDS $!:$!"
kill ${CHPROCIDS:-$!}
if [ $? -eq 0 ];then
echo "Sub-Processes killed."
fi
}
SetTimer(){
DEF_TIME=${1:-10}
if [ $DEF_TIME -ne 0 ];then
echo "in settimer1 $$:$$ $!:$!"
sleep $DEF_TIME && kill -s 14 $$ &
CHPROCIDS="$CHPROCIDS $!"
TIMERPROC=$!
echo "in settimer2 $$:$$ $!:$!"
fi
}
UnsetTimer(){
echo "UnsetTimer :$TIMERPROC"
kill $TIMERPROC
}
#main
echo "in main start $!:$!"
trap AlarmHandler 14
SetTimer
#$PROG &
CHPROCIDS="$CHPROCIDS $!"
echo "in main end $!:$!"
wait $!
UnsetTimer
echo "All Done."
#exit 0
|
纯接分
|
me, too...
谢谢楼主!
|
那就接分吧
|
末班车。呵呵
|
谢谢 LZ