1、创建服务脚本
sudo nano /etc/init.d/tomcat
脚本内容如下:
#!/bin/bash
### BEGIN INIT INFO
# Provides: Auto_Start_Test
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Auto Start Test
# Descrption: This service is used to test auto start service
### END INIT INFO
export JAVA_HOME=/cloud/jdk
export JRE_HOME=$JAVA_HOME/jre
export CATALINA_HOME=/cloud/tomcat9.0
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib/rt.jar
export PATH=$JAVA_HOME/bin:$CATALINA_HOME/bin:$JRE_HOME/bin:$PATH
case "$1" in
start)
${CATALINA_HOME}/bin/catalina.sh start
exit 0
;;
stop)
${CATALINA_HOME}/bin/catalina.sh stop
exit 0
;;
*)
echo "Usage: /etc/init.d/tomcat {start|stop}"
exit 1
;;
esac
exit 0
2、设置权限
sudo chmod 777 /etc/init.d/tomcat
3、将该服务添加到自启动项
sudo update-rc.d tomcat defaults
sudo systemctl enable tomcat.service
4、附录
功能 | 指令 |
---|---|
使httpd服务自动启动 | systemctl enable httpd.service |
使httpd服务不自动启动 | systemctl disable httpd.service |
检查服务状态(服务详细信息) | systemctl status httpd.service |
检查服务状态(仅显示是否 Active) | systemctl is-active httpd.service |
已启动的服务 | systemctl list-units --type=service |
启动某服务 | systemctl start httpd.service |
停止某服务 | systemctl stop httpd.service |
重启某服务 | systemctl restart httpd.service |