在 systemd 服务脚本中创建 PID 文件时遇到问题

在 systemd 服务脚本中创建 PID 文件时遇到问题

我正在尝试在 Debian 上安装 RipRight,但似乎没有任何预构建的软件包。我无法让 systemd 脚本运行以启动/停止作为守护进程运行的 RipRight,因为它无法将 PID 文件写入/run

我进行了常规的 configure/make/make install。我还创建了一个ripright用户/组并将其添加ripright到该cdrom组中。

这是我放置的 systemd 脚本/etc/systemd/system/ripright.service

[Unit]
Description=RipRight

[Service]
Type=forking
PrivateTmp=yes
User=ripright
Group=ripright

RuntimeDirectory=ripright
RuntimeDirectoryMode=0750

ExecStart=/usr/local/bin/ripright \
    --daemon \
    --w32-filenames \
    --require-art \
    --folder-art folder.png \
    --output-file "%B/%D/%C - %N %T.flac" \
    "/opt/ripright/data"
PIDFile=/var/run/ripright/ripright.pid

[Install]
WantedBy=multi-user.target

RuntimeDirectory我使用脚本中最近添加的指令创建了一个/run/ripright文件夹,并ripright以所有者身份运行。运行以下命令时会创建此目录:

# systemctl daemon-reload
# systemctl start ripright

在单独的窗口中:

# ls -lhrt /run
...
drwxr-x---  2 ripright ripright   40 Jan  5 20:52 ripright
drwxr-xr-x 16 root     root      400 Jan  5 20:52 systemd
# ls -lahrt /run/ripright
total 0
drwxr-xr-x 16 root     root     540 Jan  5 20:52 ..
drwxr-x---  2 ripright ripright  40 Jan  5 20:52 .
# su - ripright
$ cd /run/ripright
$ pwd
/run/ripright
$ echo test > one.txt
$ cat one.txt
test
$ rm one.txt
$ exit

我认为我的systemctl start命令没有返回,而是挂起了。大约一分钟后,它超时并显示:

# systemctl start ripright
Job for ripright.service failed. See 'systemctl status ripright.service' and 'journalctl -xn' for details.

以下是推荐命令的输出:

# systemctl status ripright.service
● ripright.service - RipRight
   Loaded: loaded (/etc/systemd/system/ripright.service; enabled)
   Active: failed (Result: timeout) since Thu 2017-01-05 20:54:40 EST; 55s ago
  Process: 35396 ExecStart=/usr/local/bin/ripright --daemon --w32-filenames --require-art --folder-art folder.png --output-file %B/%D/%C - %N %T.flac /opt/ripright/data (code=exited, status=0/SUCCESS)
 Main PID: 33287 (code=killed, signal=TERM)

Jan 05 20:53:10 ripperd ripright[35397]: Started daemon mode (v0.11)
Jan 05 20:53:10 ripperd ripright[35398]: Waiting for a CD (/dev/cdrom)
Jan 05 20:54:40 ripperd systemd[1]: ripright.service start operation timed out. Terminating.
Jan 05 20:54:40 ripperd systemd[1]: Failed to start RipRight.
Jan 05 20:54:40 ripperd systemd[1]: Unit ripright.service entered failed state.

# journalctl -xn
-- Logs begin at Thu 2017-01-05 00:30:29 EST, end at Thu 2017-01-05 20:54:40 EST. --
Jan 05 20:52:00 ripperd ripright[35380]: Waiting for a CD (/dev/cdrom)
Jan 05 20:52:59 ripperd su[35385]: Successful su for ripright by root
Jan 05 20:52:59 ripperd su[35385]: + /dev/pts/1 root:ripright
Jan 05 20:52:59 ripperd su[35385]: pam_unix(su:session): session opened for user ripright by vagrant(uid=0)
Jan 05 20:53:10 ripperd ripright[35397]: Started daemon mode (v0.11)
Jan 05 20:53:10 ripperd ripright[35398]: Waiting for a CD (/dev/cdrom)
Jan 05 20:53:33 ripperd su[35385]: pam_unix(su:session): session closed for user ripright
Jan 05 20:54:40 ripperd systemd[1]: ripright.service start operation timed out. Terminating.
Jan 05 20:54:40 ripperd systemd[1]: Failed to start RipRight.
-- Subject: Unit ripright.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit ripright.service has failed.
-- 
-- The result is failed.
Jan 05 20:54:40 ripperd systemd[1]: Unit ripright.service entered failed state.

如果我注释掉 systemd 服务脚本中的 PIDFile 指令:

#PIDFile=/var/run/ripright/ripright.pid

然后我没有遇到任何问题,但也没有针对分叉服务类型推荐的 PID 文件:

# systemctl daemon-reload
# systemctl start ripright
# ps -ef | grep ripright
ripright  35438      1  0 21:03 ?        00:00:00 /usr/local/bin/ripright --daemon --w32-filenames --require-art --folder-art folder.png --output-file %B/%D/%C - ripright %T.flac /opt/ripright/data
ripright  35439  35438  0 21:03 ?        00:00:00 /usr/local/bin/ripright --daemon --w32-filenames --require-art --folder-art folder.png --output-file %B/%D/%C - ripright %T.flac /opt/ripright/data
root      35442  31942  0 21:03 pts/0    00:00:00 grep ripright
root@ripperd:~# systemctl status ripright
● ripright.service - A minimal CD ripper for Linux modeled on autorip.
   Loaded: loaded (/etc/systemd/system/ripright.service; enabled)
   Active: active (running) since Thu 2017-01-05 21:03:11 EST; 13s ago
  Process: 35437 ExecStart=/usr/local/bin/ripright --daemon --w32-filenames --require-art --folder-art folder.png --output-file %B/%D/%C - %N %T.flac /opt/ripright/data (code=exited, status=0/SUCCESS)
 Main PID: 35438 (ripright)
   CGroup: /system.slice/ripright.service
           ├─35438 /usr/local/bin/ripright --daemon --w32-filenames --require...
           └─35439 /usr/local/bin/ripright --daemon --w32-filenames --require...

Jan 05 21:03:11 ripperd ripright[35438]: Started daemon mode (v0.11)
Jan 05 21:03:11 ripperd ripright[35439]: Waiting for a CD (/dev/cdrom)
# ls -la /run/ripright
total 0
drwxr-x---  2 ripright ripright  40 Jan  5 21:04 .
drwxr-xr-x 16 root     root     540 Jan  5 21:04 ..
# systemctl stop ripright

有趣的是,当我重新引入并PIDFiles注释掉ripright 并以 root 身份运行它时,守护进程重新加载然后启动脚本仍然挂起:UserGroup

#User=ripright
#Group=ripright
PIDFile=/var/run/ripright/ripright.pid

User如果我离开并Group注释掉并设置为直接在 /run 目录中生成 PID 文件,也会发生同样的事情PIDFile(注意 /var/run 只是 Debian Jessie 中 /run 的符号链接):

#User=ripright
#Group=ripright
#PIDFile=/var/run/ripright/ripright.pid
PIDFile=/run/ripright.pid

请注意,在所有情况下,我都可以在挂起期间按 Ctrl+C,而 ripright 守护进程将继续运行;但是,如果我让启动超时,它将在打印错误并返回之前停止守护进程。

我也经历过这个帖子及其注释。最初使用的方法也ExecStartPre得到了相同的结果;我没有取得任何进展,tmpfiles.d因为我找不到任何关于如何在不重启的情况下使更改生效的信息。我试过了,mount -a但似乎没有用。

答案1

如果你询问 systemd 是否会为守护进程创建 PID 文件,则它不会,如下所示:

https://www.freedesktop.org/software/systemd/man/systemd.service.html#PidFile=

PID文件=

采用指向此守护进程的 PID 文件的绝对文件名。建议对 Type= 设置为 forking 的服务使用此选项的 >。systemd 将在服务启动后读取守护进程主进程的 PID。systemd 不会写入此处配置的文件,但如果文件仍然存在,它将在服务关闭后删除该文件。

最好将其留空并尝试使用 GuessMainPID=。

答案2

遗憾的是,即使您在服务的单元文件中指定了一行,systemd 也不会为非分叉服务创建 PID 文件PIDFile=。但您可以使用一行来作弊ExecStartPost=,例如:

ExecStartPost=/bin/sh -c 'umask 022; pgrep YOURSERVICE > /var/run/YOURSERVICE.pid'

相关内容