我在/etc/init.d目录中有Oracle的关闭脚本
在“停止”命令上,它会执行以下操作:
su oracle -c "lsnrctl stop >/dev/null"
su oracle -c "sqlplus sys/passwd as sysdba @/usr/local/PLATEX/scripts/orastop.sql >/dev/null"
..
问题是当lsnrctl或sqlplus没有响应时-在这种情况下,此“停止”脚本永无休止,并且服务器无法关闭。唯一的方法-是“ kill -9”。
我想重写脚本,以便在5分钟后(例如)如果命令未完成-应该终止它。
我怎样才能做到这一点?你能给我一个例子吗?
我正在使用Linux RHEL 5.1 + bash。
最佳答案
如果能够使用 3rd 方工具,我会利用您可以从脚本调用的 3rd 方预先编写的帮助程序之一(doalarm 和 timeout 都被 the BashFAQ entry on the subject 提及)。
如果不使用此类工具自己编写此类内容,我可能会执行以下操作:
function try_proper_shutdown() {
su oracle -c "lsnrctl stop >/dev/null"
su oracle -c "sqlplus sys/passwd as sysdba @/usr/local/PLATEX/scripts/orastop.sql >/dev/null"
}
function resort_to_harsh_shutdown() {
for progname in ora_this ora_that ; do
killall -9 $progname
done
# also need to do a bunch of cleanup with ipcs/ipcrm here
}
# here's where we start the proper shutdown approach in the background
try_proper_shutdown &
child_pid=$!
# rather than keeping a counter, we check against the actual clock each cycle
# this prevents the script from running too long if it gets delayed somewhere
# other than sleep (or if the sleep commands don't actually sleep only the
# requested time -- they don't guarantee that they will).
end_time=$(( $(date '+%s') + (60 * 5) ))
while (( $(date '+%s') < end_time )); do
if kill -0 $child_pid 2>/dev/null; then
exit 0
fi
sleep 1
done
# okay, we timed out; stop the background process that's trying to shut down nicely
# (note that alone, this won't necessarily kill its children, just the subshell we
# forked off) and then make things happen.
kill $child_pid
resort_to_harsh_shutdown
关于bash - 在 bash 中杀死运行超过指定时间的进程?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1516458/