网上有很多类似的脚本,我结合自身业务作了一个整合,增加了状态、查看日志等常用命令
创建脚本并增加执行的权限
touch /etc/init.d/tomcat
chmod +x /etc/init.d/tomcat
chmod 755 /etc/init.d/tomcat
编辑脚本vi /etc/init.d/tomcat,加入以下内容
#!/bin/bash
# /etc/rc.d/init.d/tomcat
# init script for tomcat precesses
# processname: tomcat
# description: tomcat is a j2ee server
# chkconfig: 2345 90 10
# description: Start up the Tomcat servlet engine.
# Source function library.
# . /etc/init.d/functions
# source networking configuration.
# . /etc/sysconfig/network
catalina_home=/usr/local/tomcat
startup=${catalina_home}/bin/startup.sh
shutdown=${catalina_home}/bin/shutdown.sh
log="tail -f ${catalina_home}/logs/catalina.out"
tomcat_pid() {
echo `ps -ef | grep $catalina_home/bin | grep -v grep | tr -s " "|cut -d" " -f 2`;
}
pid="$(tomcat_pid)"
RETVAL=$?
case "$1" in
start)
if [ -n "$pid" ]
then
echo "tomcat is running,the process id is ${pid}..."
else
echo "starting tomcat"
$startup
fi
;;
stop)
if [ -z "$pid" ];then
echo "there is no tomcat running..."
else
echo "stoping tomcat..."
$shutdown
fi
;;
restart)
echo "restarting tomcat..."
if [ -z "$pid" ];then
echo "there is no tomcat running..."
else
echo "stoping tomcat..."
$shutdown
sleep 2
fi
echo "starting tomcat.."
$startup
;;
status)
if [ -z "$pid" ];then
echo "tomcat is not running"
else
echo "tomcat is running (*active)"
fi
;;
log)
echo "$log"
$log
;;
*)
echo "Usage: $prog {start|stop|restart|status|log}"
;;
esac
exit $RETVAL
通过chkconfig 增加开机自启动项
chkconfig --add tomcat
chkconfig tomcat on
至此,tomcat开机已自启动,另外可以使用以下命令
service tomcat start // 启动tomcat
service tomcat stop // 停止tomcat
service tomcat restart // 重启tomcat
service tomcat status // 查看tomcat的状态
service tomcat log // 查看tomcat滚动日志
增加软链接
ln -s /etc/init.d/tomcat /usr/bin/
现在可以使用以下命令了
tomcat start // 启动tomcat
tomcat stop // 停止tomcat
tomcat restart // 重启tomcat
tomcat status // 查看tomcat的状态
tomcat log // 查看tomcat滚动日志