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 |