为什么我无法用crontab启动delayed_job守护进程?

时间:2012-07-01 06:21:50

标签: ruby-on-rails delayed-job crontab reboot

我有以下脚本用于在rails上的ruby中启动延迟的作业工作

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>'

谢谢你的帮助

1 个答案:

答案 0 :(得分:3)

问题是,对于使用rron的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