我正在尝试按照此手册在 CentOS 7.4.1708 (Core) 上安装 ruby 应用程序 onetimesecret:https://github.com/onetimesecret/onetimesecret。Ruby 与 rvm 一起安装。
从 shell 运行此命令时一切正常:
/usr/local/rvm/rubies/ruby-2.3.8/bin/bundle exec thin -e dev -R config.ru -p 7143 -P /var/run/onetime/onetime.pid -l /var/log/onetime/onetime.log -d start
服务器启动并运行。
但它不适用于 systemd 单元。
[Unit]
Description=Onetime secret backend service
After=network.target
After=nginx.service
[Service]
Type=forking
RemainAfterExit=no
WorkingDirectory=/home/ots/onetimesecret
ExecStart=/usr/local/rvm/rubies/ruby-2.3.8/bin/bundle exec thin -e dev -R config.ru -p 7143 -P /var/run/onetime/onetime.pid -l /var/log/onetime/onetime.log -d start
TimeoutStopSec=10
PIDFile=/var/run/onetime/onetime.pid
Restart=on-failure
User=root
Group=root
[Install]
WantedBy=multi-user.target
Systemctl 状态 onetime.service:
● onetime.service - Onetime secret backend service
Loaded: loaded (/etc/systemd/system/onetime.service; enabled; vendor preset: disabled)
Active: failed (Result: start-limit) since Sun 2019-09-15 13:30:37 +04; 1s ago
Process: 4296 ExecStart=/usr/local/rvm/rubies/ruby-2.3.8/bin/bundle exec thin -e dev -R config.ru -p 7143 -P /var/run/onetime/onetime.pid -l /var/log/onetime/onetime.log -d start (code=exited, status=1/FAILURE)
Main PID: 2306 (code=exited, status=1/FAILURE)
Sep 15 13:30:36 vm-web-01 systemd[1]: onetime.service: control process exited, code=exited status=1
Sep 15 13:30:36 vm-web-01 systemd[1]: Failed to start Onetime secret backend service.
Sep 15 13:30:36 vm-web-01 systemd[1]: Unit onetime.service entered failed state.
Sep 15 13:30:36 vm-web-01 systemd[1]: onetime.service failed.
Sep 15 13:30:37 vm-web-01 systemd[1]: onetime.service holdoff time over, scheduling restart.
Sep 15 13:30:37 vm-web-01 systemd[1]: start request repeated too quickly for onetime.service
Sep 15 13:30:37 vm-web-01 systemd[1]: Failed to start Onetime secret backend service.
Sep 15 13:30:37 vm-web-01 systemd[1]: Unit onetime.service entered failed state.
Sep 15 13:30:37 vm-web-01 systemd[1]: onetime.service failed.
journalctl-xe:
Sep 15 13:30:36 vm-web-01 bundle[4296]: /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- bundler [LoadError]
Sep 15 13:30:36 vm-web-01 bundle[4296]: from /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
Sep 15 13:30:36 vm-web-01 bundle[4296]: from /usr/local/rvm/rubies/ruby-2.3.8/bin/bundle:10:in `<main>'
Sep 15 13:30:36 vm-web-01 bundle[4296]: from /usr/local/bin/ruby_executable_hooks:24:in `eval'
Sep 15 13:30:36 vm-web-01 bundle[4296]: from /usr/local/bin/ruby_executable_hooks:24:in `<main>'
Sep 15 13:30:36 vm-web-01 systemd[1]: onetime.service: control process exited, code=exited status=1
Sep 15 13:30:36 vm-web-01 systemd[1]: Failed to start Onetime secret backend service.
答案1
您应该使用 RVM 为您提供的包装器直接调用 thin。否则,系统最终可能会运行错误的 ruby,这确实发生了。
例如:
ExecStart=/usr/bin/bash /usr/local/rvm/wrappers/%i/thin start <your options>
答案2
我已经在 Google 上搜索到了正确答案:https://mikewilliamson.wordpress.com/2015/08/26/running-a-rails-app-with-systemd-and-liking-it/
因此,我必须将“/usr/bin/bash -lc”添加到 ExecStart 中的命令中。它将是:
ExecStart = /usr/bin/bash -lc '/usr/local/rvm/rubies/ruby-2.3.8/bin/bundle exec thin -e dev -R config.ru -p 7143 -P /var/run/onetime/onetime.pid -l /var/log/onetime/onetime.log -d start'