第一步: 通过service启动tomcat服务
如果想通过service启动一个自定义服务,只需在/etc/init.d/下建立启动脚本
启动方式:
service tomcat start/stop
建立tomcat文件,然后赋权
chmod +x /etc/init.d/tomcat
编辑tomcat文件:
#!/bin/sh -e
# chkconfig: 2345 20 80
# description: tomcat
### BEGIN TOMCAT SERVICE
### END INIT INFO
export JAVA_HOME=/usr/local/java/jdk1.8.0_201
export JRE_HOME=$JAVA_HOME/jre
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.
export CATALINA_HOME=/opt/apache-tomcat-8.5.39/
case "$1" in
start)
${CATALINA_HOME}/bin/startup.sh
echo 'tomcat start success'
exit 0
;;
stop)
${CATALINA_HOME}/bin/shutdown.sh
echo 'tomcat stop success'
exit 0
;;
debug)
${CATALINA_HOME}/bin/shutdown.sh
${CATALINA_HOME}/bin/catalina.sh run
exit 0
;;
force-reload|restart)
${CATALINA_HOME}/bin/startup.sh
${CATALINA_HOME}/bin/shutdown.sh
exit 0
;;
*)
echo "Usage: /etc/init.d/tomcat {start|stop|restart|force-reload|debug}"
exit 1
;;
esac
exit 0
注意; 需要加上# chkconfig: 2345 20 80 否则不能加入开机启动
这样我们就可以service tomcat start开启动服务了
第二步: 把服务加入开机启动
添加tomcat服务
chkconfig --add tomcat
查看服务
chkconfig --list
设置开机启动
chkconfig tomcat on