1.首先,需要為tomcat配置pid。
bin/catalina.sh
# Copy CATALINA_BASE from CATALINA_HOME if not already set [ -z "$CATALINA_BASE" ] && CATALINA_BASE="$CATALINA_HOME"
# 設置pid。一定要加在CATALINA_BASE定義后面,要不然pid會生成到/下面 CATALINA_PID="$CATALINA_BASE/tomcat.pid"
2.創建tomcat.service文件
vim /lib/systemd/system/tomcat.service
[Unit] Description=Tomcat After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking Environment="JAVA_HOME=/app/jdk1.8.0_291" PIDFile=/app/apache-tomcat-8.5.66/tomcat.pid ExecStart=/app/apache-tomcat-8.5.66/bin/startup.sh ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
[Unit] 表示這是基礎信息
- Description 是描述
- After 是在那個服務后面啟動,一般是網絡服務啟動后啟動
[Service] 表示這里是服務信息
- Type 是服務類型
- PIDFile 是服務的pid文件路徑, 開啟后,必須在tomcat的bin/catalina.sh中加入CATALINA_PID參數
- ExecStart 是啟動服務的命令
- ExecReload 是重啟服務的命令
- ExecStop 是停止服務的指令
[Install] 表示這是是安裝相關信息
- WantedBy 是以哪種方式啟動:multi-user.target表明當系統以多用戶方式(默認的運行級別)啟動時,這個服務需要被自動運行。
Environment="JAVA_HOME=/home/jdk/jdk1.7.0_79" 這里要配置環境變量,在/etc/profile中的配置在系統服務中不生效。
3.設置開機自啟
systemctl daemon-reload #刷新配置 剛剛配置的服務需要讓systemctl能識別,就必須刷新配置
systemctl enable tomcat.service
4.其它
修改tomcat.service文件后需要執行下面命令使其生效:
systemctl daemon-reload
systemctl daemon-reload
查詢tomcat 進程 ps -ef | grep "tomcat"| grep -v grep
如果多個tomcat,則拷貝到不同的目錄,使用不同的端口。tomcat.service文件名不同即可。
例如:tomcat1.service tomcat2.service tomcat3.service
啟動nginx服務
systemctl start nginx.service
設置開機自啟動
systemctl enable nginx.service
停止開機自啟動
systemctl disable nginx.service
查看服務當前狀態
systemctl status nginx.service
重新啟動服務
systemctl restart nginx.service
查看所有已啟動的服務
systemctl list-units --type=service