centos7 systemctl设置nginx、tomcat、reids开机自启

切换到/usr/lib/systemd/system目录下

创建nginx.service并编辑

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /usr/local/nginx/logs/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /usr/local/nginx/logs/nginx.pid)"

[Install]
WantedBy=multi-user.target

创建tomcat.service并编辑

[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/home/sv/apache-tomcat-8.5.71/tomcat.pid
ExecStart=/home/sv/apache-tomcat-8.5.71/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

创建redis.service并编辑

[Unit]
Description=redis-server
After=network.target

[Service]
Type=forking
ExecStart=/home/redis-5.0.14/bin/redis-server /home/redis-5.0.14/redis.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target

创建mqnamesrv.service并编辑

[Unit]
Description=rocketmq-nameserver
Documentation=http://mirror.bit.edu.cn/apache/rocketmq/
After=network.target

[Service]
Type=sample
User=root
ExecStart=/opt/rocketmq-4.9.3/bin/mqnamesrv -c /opt/rocketmq-4.9.3/conf/namesrv.properties
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
Restart=0
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

创建mqbroker.service并编辑

[Unit]
Description=rocketmq-broker
Documentation=http://mirror.bit.edu.cn/apache/rocketmq/
After=network.target

[Service]
Type=sample
User=root
#ExecStart=/opt/rocketmq/bin/mqbroker -c /opt/rocketmq/conf/2m-noslave/broker-b.properties
#/usr/local/rocketmq/conf/2m-2s-async/broker-a.properties
#ExecStart=/opt/rocketmq-4.9.3/bin/mqbroker -c /opt/rocketmq-4.9.3/conf/broker.conf
ExecStart=/opt/rocketmq-4.9.3/bin/mqbroker -n localhost:19876
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
Restart=0
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

分别加入开机自启

systemctl enable tomcat.service/nginx.service/redis.conf

注:如果服务是启动状态,需关闭服务后方可通过systemctl控制

[Unit]配置了服务的描述,规定了在network启动之后执行,
[service]配置服务的后台运行形式,pid,服务的启动,停止,重启
[install]配置了使用用户

你可能感兴趣的:(centos7 systemctl设置nginx、tomcat、reids开机自启)