我试图找出如何在调用另一个函数的脚本中后台处理一个进程,并在准备好时可靠地终止它。我是否必须将其全部转移到一个函数中?
function dosomething {
while :
do
printsomething "doing something"
done
}
function printsomething {
echo $@
}
function otherfunction {
dosomething &
TMP_PID=$!
}
function killsomething {
quit_test=`ps -p "$TMP_PID" | sed '1d' | awk '{print $1}'`
if [ "$quit_test" != "" ] ; then
(kill $TMP_PID >/dev/null)
fi
}
## MAIN
TMP_PID=""
echo "starting program"
otherfunction
sleep 5s
killsomething