无法为Puma创建systemd脚本

11

我在/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的原因是可执行文件不在路径中。但是,我想这不会是问题。

您能帮忙解释一下吗?

3个回答

14

我找到了问题,并按照下面所述更改了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 shims 中获取,同时可以在 puma 的配置文件 (config/puma.rb) 和应用程序的配置文件(config.ru) 中使用相对路径。


这个在 rvm 中应该有不同的工作方式吗?我尝试使用 rvm 中相应的路径,但仍然得到相同的结果。 - Carl
抱歉,我不知道。从未尝试过rvm,我的所有部署脚本都在rbenv中。 - Gowtham
@Carl 尝试执行 /home/ubuntu/.rvm/gems/[你的 gemset 名称]/bin/bundle - Mashhood

0

解决这个问题的一种方法是指定一个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而不是将其作为参数传递。我只是在这里展示它,以说明在puma配置中--pidfile应该与服务文件中的PIDFile相同。
至于为什么错误消息是那样的,我自己也不确定,对答案很感兴趣。

0

对于rvm用户,请尝试使用以下命令

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

[Service]
Type=simple
User=user_name
Group=user_name

WorkingDirectory=/home/user_name/apps/app_name/current
Environment=RAILS_ENV=production

ExecStart=/home/user_name/.rvm/bin/rvm ruby-3.1.2@app_name do bundle exec --keep-file-descriptors puma -C /home/user_name/apps/app_name/current/config/puma/production.rb
ExecReload=/bin/kill -USR1 $MAINPID

StandardOutput=append:/home/user_name/apps/app_name/current/log/puma_access.log
StandardError=append:/home/user_name/apps/app_name/current/log/puma_error.log
SyslogIdentifier=app_name-puma

Restart=always
KillMode=process


[Install]
WantedBy=multi-user.target


如果您没有使用 gemset,请进行更改

ExecStart=/home/user_name/.rvm/bin/rvm ruby-3.1.2@app_name ....

to

ExecStart=/home/user_name/.rvm/bin/rvm default

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接