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 |