本文轉(zhuǎn)載:http://blog.chinaunix.net/uid-24648266-id-5729891.html
CentOS7開始,從/etc/init.d腳本改為了systemctl管理服務(wù)。
親自測(cè)試成功。
1.首先,需要為tomcat配置pid。
bin/catalina.sh
[java]
view plain copy# Copy CATALINA_BASE from CATALINA_HOME if not already set
[ -z "$CATALINA_BASE" ] && CATALINA_BASE="$CATALINA_HOME"
# 設(shè)置pid。一定要加在CATALINA_BASE定義后面,要不然pid會(huì)生成到/下面
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
tomcat.pid文件與bin目錄平級(jí)。
2.創(chuàng)建tomcat.service文件
/lib/systemd/system/tomcat.service
[java]
view plain copy[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
Environment="JAVA_HOME=/home/jdk/jdk1.7.0_79"
PIDFile=/home/tomcat/apache-tomcat-7.0.47/tomcat.pid
ExecStart=/home/tomcat/apache-tomcat-7.0.47/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Environment="JAVA_HOME=/home/jdk/jdk1.7.0_79" 這里要配置環(huán)境變量,在/etc/profile中的配置在系統(tǒng)服務(wù)中不生效。
3.設(shè)置為開機(jī)啟動(dòng)
[java]
view plain copysystemctl enable tomcat.service
4.其它
修改tomcat.service文件后需要執(zhí)行下面命令使其生效:
systemctl daemon-reload
查詢tomcat 進(jìn)程 ps -ef | grep "tomcat"| grep -v grep
如果多個(gè)tomcat,則拷貝到不同的目錄,使用不同的端口。tomcat.service文件名不同即可。
例如:tomcat1.service tomcat2.service tomcat3.service
啟動(dòng)nginx服務(wù)
systemctl start nginx.service設(shè)置開機(jī)自啟動(dòng)
systemctl enable nginx.service停止開機(jī)自啟動(dòng)
systemctl disable nginx.service查看服務(wù)當(dāng)前狀態(tài)
systemctl status nginx.service重新啟動(dòng)服務(wù)
systemctl restart nginx.service查看所有已啟動(dòng)的服務(wù)
systemctl list-units --type=service