我有这个:
#!/bin/sh
handle_exit()
{
# if func2 called first then do some thing to kill func1 also
# if func1 called first then do some thing to kill func2 also
exit
}
func1()
{
for i in `seq 40`
do
echo I am in func1 $i
sleep 3
if [ "$i" = "20" ] ; then # this could be a failure condition
handle_exit
fi
done
}
func2()
{
for i in `seq 40`
do
echo I am in func2 $i
sleep 3
if [ "$i" = "10" ] ; then # this could be a failure condition
handle_exit
fi
done
}
func1 &
pidfunc1=$!
func2 &
pidfunc2=$!
我希望它如何表现:
func1
正在运行时发生错误,请拨打handle_exit
,exit
func1
和也会杀死func2 。func2
正在运行时发生错误,请拨打handle_exit
,exit
func2
和也会杀死func1 。你能帮忙吗?
答案 0 :(得分:-1)
将以下内容放在脚本的开头:
pidfunc1=0;
pidfunc2=0;
handle_exit()
{
if [ "pidfunc1" != "0" ] ; then
kill -9 $pidfunc1
echo "killed 1"
fi
if [ "pidfunc2" != "0" ] ; then
kill -9 $pidfunc2
echo "killed 2"
fi
}