0

我有以下脚本用于在 ruby​​ on rails 中启动延迟的工作人员

require 'rubygems'
require 'daemon_spawn'
$: << '.'

RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))

class DelayedJobWorker < DaemonSpawn::Base
  def start(args)
    ENV['RAILS_ENV'] ||= args.first || 'development'
    Dir.chdir RAILS_ROOT
    require File.join('config', 'environment')

    Delayed::Worker.new.start
  end

  def stop
    system("kill `cat #{RAILS_ROOT}/tmp/pids/delayed_job.pid`")
  end
end

DelayedJobWorker.spawn!(:log_file => File.join(RAILS_ROOT, "log", "delayed_job.log"),
                    :pid_file => File.join(RAILS_ROOT, 'tmp', 'pids', 'delayed_job.pid'),
                    :sync_log => true,
                    :working_dir => RAILS_ROOT)

它以这个命令开始

user@mysystem:path/to/my/application$ ruby script/delayed_job start production
DelayedJobWorker started.

我正在尝试使用 crontab 在启动时自动执行此操作
为此我制作了此脚本

#!/bin/bash
cd path/to/my/application
ruby script/delayed_job start production

这是我的 crontab

# m h  dom mon dow   command
SHELL=/bin/bash
@reboot /path/to/my/script/delayed_job_at_startup.sh

当我重新启动系统时,脚本没有成功,我从 crontab 收到此消息

X-Cron-Env: <SHELL=/bin/bash>
X-Cron-Env: <HOME=/home/finalsayan>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=finalsayan>

/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- daemon_spawn (LoadError)
    from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from script/delayed_job:24:in `<main>'

感谢您的帮助

4

1 回答 1

3

问题是,对于使用 rvm 的 cron,您需要加载 rvm 环境以获取文档,请参阅https://rvm.io/integration/cron/ cron 在启动时调用的脚本如下

#!/bin/bash
source /usr/local/rvm/environments/default

# Go to the app
APP_HOME=/path/to/my/application
cd $APP_HOME

# start the delayed jobs
ruby script/delayed_job start production 
于 2012-07-01T16:24:48.240 回答