无法为 Puma 创建系统脚本

Cannot create systemd script for Puma

我在 /etc/systemd/system/ 中创建了一个名为 "puma.service" 的服务脚本,其内容如下:

[Unit]
Description=Puma HTTP Server
After=network.target

[Service]
Type=simple

User=ubuntu

WorkingDirectory=/home/username/appdir/current

ExecStart=/bin/bash -lc "/home/username/appdir/current/sbin/puma -C /home/username/appdir/current/config/puma.rb /home/username/appdir/current/config.ru"

Restart=always

[Install]
WantedBy=multi-user.target

我启用了该服务,启动后,我从 systemctl 获得了以下日志:

● puma.service - Puma HTTP Server
   Loaded: loaded (/etc/systemd/system/puma.service; enabled; vendor preset: enabled)
   Active: inactive (dead) (Result: exit-code) since Wed 2016-12-14 10:09:46 UTC; 12min ago
  Process: 16889 ExecStart=/bin/bash -lc cd /home/username/appdir/current && bundle exec puma -C /home/username/appdir..
 Main PID: 16889 (code=exited, status=127)

Dec 14 10:09:46 ip-172-31-29-40 systemd[1]: puma.service: Main process exited, code=exited, status=127/n/a
Dec 14 10:09:46 ip-172-31-29-40 systemd[1]: puma.service: Unit entered failed state.
Dec 14 10:09:46 ip-172-31-29-40 systemd[1]: puma.service: Failed with result 'exit-code'.
Dec 14 10:09:46 ip-172-31-29-40 systemd[1]: puma.service: Service hold-off time over, scheduling restart.
Dec 14 10:09:46 ip-172-31-29-40 systemd[1]: Stopped Puma HTTP Server.
Dec 14 10:09:46 ip-172-31-29-40 systemd[1]: puma.service: Start request repeated too quickly.
Dec 14 10:09:46 ip-172-31-29-40 systemd[1]: Failed to start Puma HTTP Server.

虽然,当我在 SSH 终端中发出命令时,服务器启动并且 运行 完美。我必须在服务文件中进行任何更改吗?

注:

  1. 为了您的方便,我更改了目录名。
  2. 我做了一些研究,状态 127 的原因是可执行文件不在路径中。但是,我想这不会成为问题。

你能解释一下吗?

解决它的一种方法是指定一个 PID 文件,systemd 将查看该文件以检查服务状态。

以下是我们如何在脚本中使用它(根据您给定的示例进行改编)

ExecStart=/bin/bash -lc '/home/username/appdir/current/sbin/puma -C /home/username/appdir/current/config/puma.rb /home/username/appdir/current/config.ru --pidfile /home/username/appdir/current/tmp/pids/puma.pid'
PIDFile=/home/username/appdir/current/tmp/pids/puma.pid

请注意,您可能必须通过 -C puma.rb 文件配置 --pidfile,而不是将其作为参数传递给它。我只是在这里展示它来说明 --pidfile(在 puma 配置中)应该与服务文件中的 PIDFile 相同。

至于为什么会出现这样的错误信息,我自己也不清楚,也很想知道答案。

我发现了问题并按如下所述更改了 ExecStart,它非常有效:

ExecStart=/home/username/.rbenv/shims/bundle exec puma -e production -C ./config/puma.rb config.ru
PIDFile=/home/username/appdir/shared/tmp/pids/puma.pid

bundle 应该取自 rbenv 垫片,puma 的配置文件 (config/puma.rb) 和应用程序的配置文件 (config.ru) 可以在相对路径中给出。