命令卡住不返回,套一层 timeout 兜底。
timeout 30s 命令,超时先发 TERM 信号。
进程不理 TERM 就加 -k,宽限几秒再 KILL。
脚本里判断退出码,124 表示已超时。
需要真实退出码就加 –preserve-status。
# 最多跑 30 秒,超时先发 TERM
timeout 30s ./slow_task.sh
# 10 秒宽限还不退出,补 KILL
timeout -k 10s 30s ./slow_task.sh
# 脚本里区分:124 是超时,137 是被 KILL
if timeout 30s ./job.sh; then
echo ok
else
case $? in
124) echo "timeout" ;;
137) echo "killed" ;;
esac
fi
THE END






暂无评论内容