![通过 cron(root)启动的 bash 脚本未按预期结束](https://linux22.com/image/1162784/%E9%80%9A%E8%BF%87%20cron%EF%BC%88root%EF%BC%89%E5%90%AF%E5%8A%A8%E7%9A%84%20bash%20%E8%84%9A%E6%9C%AC%E6%9C%AA%E6%8C%89%E9%A2%84%E6%9C%9F%E7%BB%93%E6%9D%9F.png)
我有如下脚本,我正尝试将其作为 root cron 作业运行。该脚本启动 mycommand,然后启动另一个进程。当满足条件时,我希望脚本在退出之前终止 mycommand 及其子进程。现在,这似乎在终端上起作用,子进程在满足条件时结束。但是当脚本作为 root cron 作业启动时,mycommand 会继续运行,直到我手动终止它。我做错了什么?
#!/bin/bash
# function to trap and clean exit
function clean_exit
{
PGID=`ps xao pgid,command,pid | grep 'mycommand'| sed 's/ .*//'`
kill -9 -$PGID
echo "process group killed: "$PGID
}
trap clean_exit SIGINT SIGTERM EXIT
sudo mycommand $1 > output.txt &
while sleep 1; do
# check output.txt every second
if [ condition met]; then
exit 1;
fi;
done
crontab行如下:
30 10 * * * bash path/to/myscript.sh