我在/etc/init.d dir上有一个关闭Oracle的关闭脚本,它执行"stop"命令:
su oracle -c "lsnrctl stop >/dev/null"
su oracle -c "sqlplus sys/passwd as sysdba @/usr/local/PLATEX/scripts/orastop.sql >/dev/null"
Run Code Online (Sandbox Code Playgroud)
..问题是当lsnrctl或sqlplus没有响应时 - 在这种情况下,这个"停止"脚本永远不会结束,服务器无法关闭.唯一的方法 - 就是"杀 - 9"那个.
我想重写脚本,以便在5分钟后(例如)如果命令没有完成 - 它应该被终止.
我怎么能做到这一点?你能举个例子吗?我在Linux RHEL 5.1 + bash下.
如果能够使用第三方工具,我会利用您可以从脚本调用的第三方预先编写的帮助程序之一(关于该主题的BashFAQ条目提及了doalarm和timeout).
如果在不使用这些工具的情况下自己编写这样的东西,我可能会做如下的事情:
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
Run Code Online (Sandbox Code Playgroud)
哇,这是一个复杂的解决方案.这里更简单.您可以跟踪PID并在以后将其终止.
my command & #where my command is the command you want to run and the & sign backgrounds it.
PID=$! #PID = last run command.
sleep 120 && doProperShutdown || kill $PID #sleep for 120 seconds and kill the process properly, if that fails, then kill it manually.. this can be backgrounded too.
Run Code Online (Sandbox Code Playgroud)