systemctl使用方式
systemctl nginx服务配置和常用命令介绍
以nginx为例,介绍如何使用systemctl 把程序做成服务。
配置
创建服务配置文件 nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true
[Install]
WantedBy=multi-user.target
将nginx.service 放到 /lib/systemd/system/ 目录,systemctl会在这个目录查找配置文件。
sudo mv nginx.service /lib/systemd/system/
常用命令
# 开启自动启动
sudo systemctl enable nginx
# 取消开机启动
sudo systemctl disable nginx
# 启动服务
sudo systemctl start nginx
# 停止服务
sudo systemcl stop nginx
# 查看服务配置
sudo systemctl cat nginx
最后修改于 2022-05-22
此篇文章的评论功能已经停用。