Centos7 使用Systemd开机自启动nginx+mysql+tomcat
nginx
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=Seuic Nginx Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
[Install]
WantedBy=multi-user.target
设置开机自启动systemctl enable nginx.service
mysqld
vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=Seuic MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=syslog.target network.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
[Install]
WantedBy=multi-user.target
设置开机自启动systemctl enable mysqld.service
tomcat
vim /usr/local/apache-tomcat-9.0.20/bin/catalina.sh
# Get standard environment variables
PRGDIR=`dirname "$PRG"`
# Only set CATALINA_HOME if not already set
[ -z "$CATALINA_HOME" ] && CATALINA_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`
# Copy CATALINA_BASE from CATALINA_HOME if not already set
[ -z "$CATALINA_BASE" ] && CATALINA_BASE="$CATALINA_HOME"
#添加的pid
# setting tomcat.pid
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
vim /usr/lib/systemd/system/tomcat.service
[Unit]
Description=Seuic Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
Environment="JAVA_HOME=/usr/local/jdk1.8.0_161"
Environment="CATALINA_HOME=/usr/local/apache-tomcat-9.0.20"
PIDFile=/usr/local/apache-tomcat-9.0.20/tomcat.pid
ExecStart=/usr/local/apache-tomcat-9.0.20/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
修改.service文件后需要执行下面命令使其生效:systemctl daemon-reload
启动tomcat服务systemctl start tomcat.service
设置开机自启动systemctl enable tomcat.service
停止开机自启动systemctl disable tomcat.service
查看服务当前状态systemctl status tomcat.service
重新启动服务systemctl restart tomcat.service
查看所有已启动的服务systemctl list-units --type=service