systemd 服务无法为应用运行

systemd 服务无法为应用运行

我想为“RAILS_ENV=staging bundle exec rake jobs:work”创建 systemd

我有这个:

$ sudo cat /etc/systemd/system/my_bg_jobs.service 

[Unit]
Description=bg jobs
After=syslog.target

[Service]
Type=simple
User=my_user123
WorkingDirectory=/home/my_user123/projects/ruby/my_app123
ExecStart=/home/my_user123/.gem/ruby/2.3.4/bin/bundle exec rake jobs:work
Restart=on-abort
Environment=RAILS_ENV=staging
SyslogIdentifier=my_bg_jobs

[Install]
WantedBy=multi-user.target

当我运行它时:

  $ sudo systemctl status my_bg_jobs
       Loaded: loaded (/etc/systemd/system/my_bg_jobs.service; disabled; vendor preset: disabled)
       Active: failed (Result: exit-code) since Tue 2018-07-10 11:07:29 +07; 1s ago
       Process: 24138 ExecStart=/home/my_user123/.gem/ruby/2.3.4/bin/bundle exec rake jobs:work (code=exited, status=1/FAILURE)
       Main PID: 24138 (code=exited, status=1/FAILURE)


      [..........]

     systemd[1]: Started bg jobs.
     my_bg_jobs[24138]: /home/my_user123/.rubies/ruby-2.3.4/lib/ruby/2.3.0/rubygems.rb:241:in `bin_path': can't find gem bundler (>= 0.a) (Gem::GemNot>
     my_bg_jobs[24138]:         from /home/my_user123/.gem/ruby/2.3.4/bin/bundle:22:in `<main>'

     systemd[1]: my_bg_jobs.service: Main process exited, code=exited, status=1/FAILURE
     systemd[1]: my_bg_jobs.service: Failed with result 'exit-code'.

为什么会这样?如何解决?

我已经安装好了捆绑器:

$ which bundler

/home/my_user123/.gem/ruby/2.3.4/bin/bundle

rake 作业本身,无需 systemd,正常运行时即可完美运行。

答案1

您的环境中缺少某些内容。当然是 的 PATH 条目/home/my_user123/.gem/ruby/2.3.4/bin,也可能是其他变量。

尝试使用 启动您的应用程序env -i。然后,您可以将必要的环境变量添加到 env 命令行和 systemd 单元文件,或者您可以创建一个 shell 脚本来设置环境,然后调用您要执行的命令。

相关内容