对ruby服务设置自动重启,出错并解决(记录)

来源:互联网 发布:ubuntu如何合理分区 编辑:程序博客网 时间:2024/06/09 17:47

rails之类的服务通过systemd设置网上资料蛮多的,下面内容也许只能做简单参考。
日前,安装一个静态文档模板slate,该程序是使用通过ruby写的,slate项目github地址:https://github.com/lord/slate 。安装过程,所需依赖版本,在此网址均有描述,在此不再复述。
本来安装完并没有考虑让其自动重启,但因其挂掉频率过高,故考虑之。
在/etc/systemd/system目录下编辑文件:

# cat slate-server.service[Unit]Description=slate_server container[Service]Restart=alwaysWorkingDirectory=/home/slate   ExecStart=/usr/local/rvm/gems/ruby-2.3.0/bin/bundle exec /usr/local/rvm/gems/ruby-2.3.0/bin/middleman server[Install]WantedBy=default.target

如上所示Restart=always意为一直重启,WorkingDirectory=/home/slate意为在该目录下启动此服务(bundle需要在此目录下查找Gemfile文件,middleman需要在此目录下查找config.rb文件),ExecStart=/usr/local/rvm/gems/ruby-2.3.0/bin/bundle exec /usr/local/rvm/gems/ruby-2.3.0/bin/middleman server意思更明显,意为启动时使用此命令。书写完毕后,执行如下命令:

# systemctl daemon-reload# systemctl enable slate-server.service# systemctl start slate-server.service# systemctl status slate-server.service

会发现启动失败,通过如下命令,查看详细错误报告:

# journalctl -xu slate-server.service

查看后你会发现启动时不知道ruby路径,变添加路径:

# cat slate-server.service[Unit]Description=slate_server container[Service]Restart=alwaysWorkingDirectory=/home/slate Environment=PATH=/usr/local/rvm/gems/ruby-2.3.0/bin:/usr/local/rvm/gems/ruby-2.3.0@global/bin:/usr/local/rvm/rubies/ruby-2.3.0/bin:/usr/local/rvm/binExecStart=/usr/local/rvm/gems/ruby-2.3.0/bin/bundle exec /usr/local/rvm/gems/ruby-2.3.0/bin/middleman server[Install]WantedBy=default.target

执行之前的systemctl命令,还是发现执行不了,没辙了,只得google了。。。。。
找到了这么个网址:https://github.com/mperham/sidekiq/blob/master/examples/systemd/sidekiq.service 看到了这么一句ExecStart=/bin/bash -lc 'bundle exec sidekiq -e production' 而且并没有加PATH路径,故先尝试吧,再找原因,修改slate-server.service文件内容如下:

# cat slate-server.service[Unit]Description=slate_server container[Service]Restart=alwaysWorkingDirectory=/home/slateExecStart=/bin/bash -lc 'bundle exec middleman server'[Install]WantedBy=default.target

执行之前的systemctl命令发现执行成功了!

对此,ExecStart为什么这么写,就需要man了。man bash 查找-l 和-c 选项意义。man文档对此介绍的内容较多,在此,不便复述,若不愿查看篇幅较长的英文文档,可百度man bash 中文版

1 0
原创粉丝点击