bash 如何在 Tomcat6 的 init 脚本中为“status”命令设置退出代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24416902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set exit codes for "status" command in init script for Tomcat6
提问by invict_us
I'm installing Tomcat6 and using the following for /etc/init.d/tomcat6:
我正在安装 Tomcat6 并为 /etc/init.d/tomcat6 使用以下内容:
#!/bin/bash
# description: Tomcat6 service
# processname: java
# chkconfig: - 99 1
## Note: CATALINA_HOME and CATALINA_PID are set elsewhere.##
# Source function library.
. /etc/init.d/functions
# Source sysconfig for tomcat6
if [ -f /etc/sysconfig/tomcat6 ]; then
. /etc/sysconfig/tomcat6
fi
[ -d "$CATALINA_HOME" ] || { echo "Tomcat requires $CATALINA_HOME."; exit 1; }
case in
start|stop|run)
if su $TOMCAT_USER bash -c "cd $CATALINA_HOME/logs; $CATALINA_HOME/bin/catalina.sh "; then
echo -n "Tomcat successful"
[ == "stop" ] && rm -f $CATALINA_PID
else
echo -n "Error in Tomcat : $?"
fi
;;
restart)
0 program is running or service is OK
1 program is dead and /var/run pid file exists
2 program is dead and /var/lock lock file exists
3 program is not running
4 program or service status is unknown
5-99 reserved for future LSB use
100-149 reserved for distribution use
150-199 reserved for application use
200-254 reserved
start
#!/bin/bash
# description: Tomcat6 service
# processname: java
# chkconfig: - 99 1
# Source function library.
. /etc/init.d/functions
# Source sysconfig for tomcat6
if [ -f /etc/sysconfig/tomcat6 ]; then
. /etc/sysconfig/tomcat6
fi
[ -d "$CATALINA_HOME" ] || { echo "Tomcat requires $CATALINA_HOME."; exit 1; }
exit_var=0
case in
start|stop|run)
if su $TOMCAT_USER bash -c "cd $CATALINA_HOME/logs; $CATALINA_HOME/bin/catalina.sh "; then
echo -n "Tomcat successful"
[ == "stop" ] && rm -f $CATALINA_PID
else
echo -n "Error in Tomcat : $?"
exit_var=1
fi
;;
restart)
status)
if [ -f "$CATALINA_PID" ]; then
read kpid < "$CATALINA_PID"
if ps --pid $kpid 2>&1 1>/dev/null; then
echo "##代码## is already running at ${kpid}"
## Fixes issue with Chef not starting a stopped service.
exit_var=3 ## this is the condition of already running
else
echo "$CATALINA_PID found, but $kpid is not running"
exit_var=4
fi
unset kpid
else
echo "##代码## is stopped"
exit_var=5 # (renumbered 5 set as you desire)
fi
;;
esac
exit $exit_var
start
##代码## stop
;;
status)
if [ -f "$CATALINA_PID" ]; then
read kpid < "$CATALINA_PID"
if ps --pid $kpid 2>&1 1>/dev/null; then
echo "##代码## is already running at ${kpid}"
exit_var=0
else
echo "$CATALINA_PID found, but $kpid is not running"
exit_var=4
fi
unset kpid
else
echo "##代码## is stopped"
exit_var=3 # Fixes issue with Chef not starting a stopped service.
fi
;;
esac
exit $exit_var
stop
;;
status)
if [ -f "$CATALINA_PID" ]; then
read kpid < "$CATALINA_PID"
if ps --pid $kpid 2>&1 1>/dev/null; then
echo "##代码## is already running at ${kpid}"
else
echo "$CATALINA_PID found, but $kpid is not running"
fi
unset kpid
else
echo "##代码## is stopped"
fi
;;
esac
exit 0
The problem, as noted in this related ticket, is that Chef checks the "status" of a service and will not start it if the "status" command returns an exit code of "0". Which it always does because the script itself completes successfully, regardless of whether the service is running or not.
正如此相关票证中所述,问题在于 Chef 检查服务的“状态”,如果“状态”命令返回退出代码“0”,则不会启动它。它总是这样做,因为脚本本身成功完成,无论服务是否正在运行。
I need to adapt my init script to return an exit code of 3 if the service is not running, per the guidelines for Init scripts posted here:
根据此处发布的 Init 脚本指南,如果服务未运行,我需要调整我的 init 脚本以返回退出代码 3 :
##代码##I modified my initial script to:
我将初始脚本修改为:
##代码##But those aren't ACTUALLY changing the exit codes for the script. How can I set different exit codes for different case scenarios?
但这些实际上并没有改变脚本的退出代码。如何为不同的情况设置不同的退出代码?
Version Info:
版本信息:
- OS: CentOS 6.5
- Chef: 10.20
- Tomcat: 6.0.39
- 操作系统:CentOS 6.5
- 厨师:10.20
- 雄猫:6.0.39
回答by David C. Rankin
You have the right idea, but you have exit_var=3
in the wrong place. I have placed it below to equal 3
for the status
when it is already running:
你有正确的想法,但你有exit_var=3
错误的地方。我已经把它放在下面平等3
的status
,当它已经在运行: