为什么 systemd 的服务 PID 文件出现问题?

为什么 systemd 的服务 PID 文件出现问题?

我正在嵌入式硬件上运行 Linux 的自定义 Yocto 版本。我已经安装了 nginx,但它无法正常启动。

systemctl start nginx.service失败

当我尝试启动服务时,它成功创建了 PID 文件,并且 nginx 启动(我可以通过浏览器连接)。一切正常。然后失败,nginx 停止,PID 文件被删除。

我已经验证了 PID 文件中的 PID 是正确的(在它存在的 10 秒左右期间)。

以下是结果journalctl --xe

-- Unit nginx.service has begun starting up.
Mar 03 13:40:34 imx8mq-var-dart systemd[1]: nginx.service: Permission denied while opening PID file or unsafe symlink chain: /run/nginx/nginx.pid
Mar 03 13:42:04 imx8mq-var-dart systemd[1]: nginx.service: Start operation timed out. Terminating.
Mar 03 13:42:04 imx8mq-var-dart systemd[1]: nginx.service: Failed with result 'timeout'.
Mar 03 13:42:04 imx8mq-var-dart systemd[1]: Failed to start Nginx Server.
-- Subject: Unit nginx.service has failed

/lib/systemd/service/nginx.service看起来像这样:

[Unit]
Description=Nginx Server
After=network.target
[Service]
Type=forking
PIDFile=/run/nginx/nginx.pid
ExecStart=/usr/sbin/nginx
ExecStop=/usr/sbin/nginx -s stop
ExecReload=/usr/sbin/nginx -s reload
[Install]
WantedBy=multi-user.target

目录的权限是:

drwxr-xr-x   13 root     root           340 Feb 27 14:42 /run
drwxr-xr-x    7 root     root           140 Mar  3 13:42 /run/nginx

这些是 PID 文件存在时的权限:

-rw-r--r--    1 root     root             5 Mar  3 13:59 /run/nginx/nginx.pid

/run 是一个 tmpfs

tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)

答案1

删除已安装的服务

systemctl disable nginx

并尝试用此模板替换 nginx 服务文件

# /etc/systemd/system/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

相关内容