centos7下如何自行在systemctl添加系统服务

以nginx为例

vim /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

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t 
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

然后:

systemctl daemon-reload
#启动:
systemctl start nginx
# 重启
systemctl restart nginx
#停止
systemctl stop nginx
#设置开启启动
systemctl enable nginx
# 查看服务是否启动,端口是否打开
netstat -tulnp

 

自定义一个web服务:xxx

vim /usr/lib/systemd/system/xxx.service

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

[Service]
Type=forking
ExecStart=/usr/local/bin/uwsgi --ini /opt/server/uwsgi.ini  # 自定义自己服务启动命令
User=root
Group=root

[Install]
WantedBy=multi-user.target



#启动:
systemctl start xxx
#设置开启启动
systemctl enable xxx

 

你可能感兴趣的:(运维)