Cron jobs in Ruby

Is Rails's "delayed_job" for cron task really?

delayed_job is at http://github.com/collectiveidea/delayed_job

Can delayed_job have the ability to do cron task? Such as running a script every night at 1am. Or run a script every 1 hour.

If not, what are the suitable gems that can do that? And can it be monitored remotely using a browser, and have logging of success and error?

Source: (StackOverflow)

whenever gem have cronjob on only one machine?

We have a large deployment of around a dozen servers. We'd like to use the Whenever gem but I can't figure out a way to say which machine the cron jobs should go on! We only want these jobs to run on our server that does background jobs.

Is there a way to specify this?

Source: (StackOverflow)


Mountable engine with cron (whenever gem)

Is it possible to use cron (via the whenever gem) to run tasks directly on mountable engine models. The cron I think would not be able to start from the main app since mountable engines are supposed to be isolated.

I am able to use whenever within a normal rails app and it works great but I have a need to run tasks in an engine style way.


Source: (StackOverflow)

Rails cron whenever, bundle: command not found

I am trying to use whenever to execute a rake task onces a day. Im getting this error

/bin/bash: bundle: command not found
/home/app/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Could not find bundler (>= 0) amongst [minitest-1.6.0, rake-0.8.7, rdoc-2.5.8] (Gem::LoadError)
        from /home/app/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:256:in `to_spec'
        from /home/app/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems.rb:1210:in `gem'
        from /home/app/.rvm/gems/ruby-1.9.2-p180/bin/bundle:18:in `<main>'

Here is my crontab

# Begin Whenever generated tasks for: /home/af/www/app/releases/20120216172204/config/schedule.rb

0 0 * * * /bin/bash -l -c 'cd /home/af/www/app/releases/20120216172204 && rvm 1.9.1-p180; RAILS_ENV=production /home/af/.rvm/gems/ruby-1.9.2-p180/bin/bundle exec rake daily:stats --silent >> /home/af/www/app/releases/20120216172204/log/cron.log 2>&1'

# End Whenever generated tasks for: /home/af/www/app/releases/20120216172204/config/schedule.rb

I'm at a loss as to why it isn't working. If I run the command:

cd /home/af/www/app/releases/20120216172204 && rvm 1.9.1-p180; RAILS_ENV=production /home/af/.rvm/gems/ruby-1.9.2-p180/bin/bundle exec rake daily:stats --silent >> /home/af/www/app/releases/20120216172204/log/cron.log 2>&1

It works fine, not sure whats going on here.

Source: (StackOverflow)

How to correctly implement recurring tasks with time/frequency set by user

Users subscribe to emails containing the last videos, but they also set when to get those emails.

Subscription(user_id, frequency, day, time, time_zone)

user_id  |  frequency  |  day    |  time   |  time_zone
1        |  daily      |  null   |  16:00  |  GMT
2        |  weekly     |  friday |  11:00  |  UTC
3        |  weekly     |  monday |  18:00  |  EST

How can we send the emails at the exact time and frequency chosen by users in their time zone without screwing up (like sending double emails or missing time)

The only frequencies are daily and weekly, if daily then the day is null.

I use redis as a database for this, let me know how to do this the right way!

Source: (StackOverflow)

Rails 3 - Whenever gem error: /usr/bin/env: ruby: No such file or directory

When using the 'whenever gem', I get an error in the log:

/usr/bin/env: ruby: No such file or directory

It works when I run the job manually. I've installed everything with RVM.

I've used the which command to find where my Ruby installation is, and I get:

kevin@lovely:/opt/personal$ which ruby

and I've checked my $PATH variable, where it returns:

kevin@lovely:/opt/personal$ echo $PATH

I believe this should be set up right, but I'm probably wrong since it doesn't work. Can anyone point me in the right direction?

If you're interested, this is what my whenever crontab output is:

# Begin Whenever generated tasks for: rss
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /opt/personal && script/rails runner -e development '\''FeedEntry.update_from_feed("http://lovely/blog/feed/")'\'' >> /opt/personal/log/feedzirra.log 2>&1'

Source: (StackOverflow)

whenever gem schedule.rb file: doesn't recongize RAILS_ROOT variable

In schedule.rb file, the statement:

require "#{RAILS_ROOT}/config/environment.rb"

 every "10 10 2 * * *" do
      command "mysqldump -u #{@db_username} -p#{@db_password} --single-transaction #{@db_name} > #{@backup_Path}/#{@db_name}.sql 2> log/error_crontab.log"

When i try to execute the whenever cmd from terminal, getting the following error:

 config/schedule.rb:48:in `initialize': uninitialized constant Whenever::JobList::RAILS_ROOT (NameError)
    from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/job_list.rb:19:in `instance_eval'
    from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/job_list.rb:19:in `initialize'
    from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever.rb:16:in `new'
    from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever.rb:16:in `cron'
    from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/command_line.rb:40:in `run'
    from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/command_line.rb:7:in `execute'
    from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/bin/whenever:38:in `<top (required)>'
    from /usr/local/bin/whenever:19:in `load'
    from /usr/local/bin/whenever:19:in `<main>'

i am using the require statement to get the dynamic values from the form to schedule the job. Please help to solve this issue?

Note: i have seen the following stackoverflow queries: How to detect Rails environment inside whenever

Following this thread to get dynamic values, but facing problem with require statement. Rails - Whenever gem - Dynamic values

Ruby/Rails - Whenever gem - Loop cron tasks

config file in schedule.rb with Rails Whenever gem?

Thanks, Sudhir C.N.

Source: (StackOverflow)

whenever gem: I set :output but the logfile doesn't show up where I'd expect it to

In my schedule.rb file I have the following lines:

set :output, '/log/cron_log.log'

every 5.minutes do
  command 'echo "hello"'

I ran whenever -w as suggested in this question Rails, using whenever gem in development, and I assume the cronfile is written and running. (I tried restarting the Rails server as well.)

And when I run $ crontab -l I see the following:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash
-l -c 'echo "hello" >> /log/cron_log.log 2>&1'

But I can't find the log file. I checked in rails_project/log, ~/log and /log to no avail. On OSX btw.

How can I get the log file to be written to the rails project log folder?

Source: (StackOverflow)

sidekiq+whenever+unicorn uninitialized constant ClassWorker production env

I use Rails 4, sidekiq 2.13.1, whenever, cron script run every 2 minutes. No errors during the first execute but all next get errors. I got this error only in production env. My worker locate in app/workers. I've included it by following string in application.rb:

config.eager_load_paths += %W(#{config.root}/app/workers) 


2013-08-15T12:34:05Z 31102 TID-oh1d0 WARN: {"retry"=>true, "queue"=>"default", "class"=>"AllGlobalWorker", "args"=>[], "jid"=>"c8f5827813277c890b4a621e", "enqueued_at"=>1376570045.3903732}
2013-08-15T12:34:05Z 31102 TID-oh1d0 WARN: uninitialized constant AllGlobalWorker
2013-08-15T12:34:05Z 31102 TID-oh1d0 /home/shared/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/inflector/methods.rb:226:in `const_get'

Any ideas for this? Thanks.

Source: (StackOverflow)

Whenever Gem Fails to Run "bundle exec" Correctly in Capistrano

I'm having problems deploying the Whenever gem to my production environment using Capistrano.

The problem is stemming from a bundle exec whenever command that's triggering some 'missing gem' issues (yet running bundle install from the shell shows that everything is in fact there!).

My feeling is that one of two things is happening: that Bundler isn't fully loading before bundle exec is being called, or that somehow there's a path issue that's running it in the wrong place.

Here's what's going on:

  * executing `deploy'
  * executing `deploy:update'
 ** transaction: start
  * executing `deploy:update_code'
    executing locally: "git ls-remote git@my-source-repository:mysource.git HEAD"
    command finished in 1847ms
  * executing "git clone -q git@my-source-repository:mysource.git /domains/myapp/releases/20130124211036 && cd /domains/myapp/releases/20130124211036 && git checkout -q -b deploy 90238bbcb993e3e7df2374ffaa13e7ed701c202e && (echo 90238bbcb993e3e7df2374ffaa13e7ed701c202e > /domains/myapp/releases/20130124211036/REVISION)"
    servers: ["myip"]
    [myip] executing command
 ** [myip :: out] Enter passphrase for key '/home/deploy/.ssh/id_rsa':
 ** [myip :: out]
    command finished in 9294ms
  * executing `deploy:finalize_update'
    triggering before callbacks for `deploy:finalize_update'
  * executing `whenever:update_crontab'
  * executing "cd /domains/myapp/releases/20130124211036 && bundle exec whenever --update-crontab My App --set environment=production --roles db"
    servers: ["myip"]
    [myip] executing command
 ** [out :: myip] Could not find carrierwave-0.5.8 in any of the sources
 ** [out :: myip] Run `bundle install` to install missing gems.
    command finished in 1967ms
*** [deploy:update_code] rolling back
  * executing "rm -rf /domains/myapp/releases/20130124211036; true"
    servers: ["myip"]
    [myip] executing command
    command finished in 998ms
failed: "rvm_path=/usr/local/rvm /usr/local/rvm/bin/rvm-shell '1.9.3' -c 'cd /domains/myapp/releases/20130124211036 && bundle exec whenever --update-crontab My App --set environment=production --roles db'" on myip

You can see that the failure is coming from this line:

bundle exec whenever --update-crontab

Which is in turn being run by my Capistrano deploy.rb.

# Your Application's "Name"
set :application, [My App]

# The URL to your application's repository
set :repository, [repo]
set :scm, :git
set :scm_passphrase, [password]

# The user you are using to deploy with (This user should have SSH access to your server)
set :user, "deploy"

# We want to deploy everything under your user, and we don't want to use sudo
set :use_sudo, false

# Where to deploy your application to.
set :deploy_to, "/domains/myapp/"

# Load RVM's capistrano plugin.    
require "rvm/capistrano"

set :rvm_ruby_string, '1.9.3'
set :rvm_type, :system  # use system-wide RVM

# Require that we use bundler to install necessary gem dependencies
require "bundler/capistrano"

set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"

# -------------------------------- Server Definitions --------------------------------
# Define the hostname of your server.  If you have multiple servers for multiple purposes, we can define those below as well.
set :server_name, [server]

# We're assuming you're using a single server for your site, but if you have a seperate asset server or database server, you can specify that here.
role :app, server_name
role :web, server_name
role :db,  server_name, :primary => true

# -------------------------------- Final Config --------------------------------
# This configuration option is helpful when using svn+ssh but doesn't hurt anything to leave it enabled always.
default_run_options[:pty] = true 

namespace :deploy do
  desc "Tell Passenger to restart the app."
  task :restart do
    run "touch #{deploy_to}current/tmp/restart.txt"

  task :start do
    run "cd #{deploy_to} && ln -s current/public public_html"
    run "touch #{deploy_to}current/tmp/restart.txt"

  task :symlink do
    run "cd #{deploy_to} && rm current ; ln -s releases/#{release_name} current"

  task :stop do
    run "rm #{deploy_to}public_html"

  namespace :web do
    task :disable do
      run "cd #{deploy_to} && rm public_html && ln -s static_site public_html"

    task :enable do
      run "cd #{deploy_to} && rm public_html && ln -s current/public public_html"

  desc "Symlink shared configs and folders on each release."
  task :symlink_shared do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
    run "ln -nfs #{shared_path}/assets #{release_path}/public/assets"

    # create symlink to uploads folder for Carrierwave resources (i.e., book cover images)
    run "ln -nfs #{shared_path}/uploads  #{release_path}/public/uploads"

  desc "Sync the public/assets directory."
  task :assets do
    system "rsync -vr --exclude='.DS_Store' public/assets #{user}@#{application}:#{shared_path}/"


after "deploy", "deploy:migrate"

after 'deploy:update_code', 'deploy:symlink_shared'

after(:setup) do
  run "rm -rf #{deploy_to}public_html"
  run "mkdir #{deploy_to}static_site"

Now, the error that I'm getting about CarrierWave is bogus - I've been using CarrierWave for ages - and the issue clearly stems (as you can see by the command that triggers the error) from either Whenever itself or Bundler.

And I'm thinking that it actually may have something to do with paths related to RVM. At any rate, the bundle exec whenever isn't working.

Any ideas?

Source: (StackOverflow)

Could not find rake 10.0.3 in any of the sources, when working with Whenever and Capistrano

I'm trying to use the gem Whenever with Capistrano over my Rails app on 3.2.8, working with RVM and a gemset for that Rails version.

I'm getting the following error: Could not find rake-10.0.3 in any of the sources (Bundler::GemNotFound)

This is the output of the error (which I logged over a file):

/home/some_user/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.2.1/lib/bundler/spec_set.rb:90:in `block in materialize': Could not find rake-10.0.3 in any of the sources (Bundler::GemNotFound)
    from /home/some_user/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.2.1/lib/bundler/spec_set.rb:83:in `map!'
    from /home/some_user/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.2.1/lib/bundler/spec_set.rb:83:in `materialize'
    from /home/some_user/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.2.1/lib/bundler/definition.rb:113:in `specs'
    from /home/some_user/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.2.1/lib/bundler/definition.rb:158:in `specs_for'
    from /home/some_user/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.2.1/lib/bundler/definition.rb:147:in `requested_specs'
    from /home/some_user/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.2.1/lib/bundler/environment.rb:23:in `requested_specs'
    from /home/some_user/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.2.1/lib/bundler/runtime.rb:11:in `setup'
    from /home/some_user/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.2.1/lib/bundler.rb:116:in `setup'
    from /home/some_user/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.2.1/lib/bundler/setup.rb:17:in `<top (required)>'
    from /home/some_user/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
    from /home/some_user/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
    from /home/some_user/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
    from /home/some_user/some_user/config/boot.rb:6:in `<top (required)>'
    from /home/some_user/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /home/some_user/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from script/rails:5:in `<main>'

I did also get the error when I was using Rake 10.0.2 (I made bundle update and get Rake 10.0.3, and I leave it for testing). Also, I noticed that the bundler is searching over @global when I think it should search over the gemset (called @r328, and is located on /home/some_user/.rvm/gems).

.rvmrc (located inside of the project)

source ~/.profile
rvm use 1.9.3@r328


if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function

schedule.rb (for testing)

set :output, "#{path}/log/cron.log"

every 1.minute do
  runner "MenuOrder.send_resume(2)"


set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"

The output of gem list

actionmailer (3.2.8)
actionpack (3.2.8)
active_utils (1.0.5)
activeadmin (0.5.0)
activemerchant (1.29.3, 1.28.0)
activemodel (3.2.8)
activerecord (3.2.8)
activeresource (3.2.8)
activesupport (3.2.8)
arbre (1.0.1)
arel (3.0.2)
authorize-net (1.5.2)
bcrypt-ruby (3.0.1)
bourbon (3.0.1, 2.1.2, 2.1.1)
builder (3.0.4)
bundler (1.2.1)
capistrano (2.13.5)
carrierwave (0.8.0, 0.7.1, 0.7.0)
chronic (0.9.0)
cocaine (0.4.2)
coffee-rails (3.2.2)
coffee-script (2.2.0)
coffee-script-source (1.4.0)
daemon_controller (1.1.0)
daemons (1.1.9)
delayed_job (3.0.4)
delayed_job_active_record (0.3.3)
devise (2.2.0, 2.1.2)
erubis (2.7.0)
execjs (1.4.0)
fastercsv (1.5.5)
fastthread (1.0.7)
formtastic (2.2.1)
has_scope (0.5.1)
highline (1.6.15)
hike (1.2.1)
i18n (0.6.1)
inherited_resources (1.3.1)
journey (1.0.4)
jquery-rails (2.1.4, 2.1.3)
json (1.7.6, 1.7.5)
kaminari (0.14.1)
mail (2.4.4)
meta_search (1.1.3)
mime-types (1.19)
money (5.1.0)
multi_json (1.5.0, 1.3.7, 1.3.6)
mysql2 (0.3.11)
net-scp (1.0.4)
net-sftp (2.0.5)
net-ssh (2.6.2)
net-ssh-gateway (1.1.0)
nokogiri (1.5.6, 1.5.5)
orm_adapter (0.4.0)
paperclip (3.4.0, 3.3.1)
passenger (3.9.1.beta, 3.0.19, 3.0.18)
polyamorous (0.5.0)
polyglot (0.3.3)
rack (1.4.3, 1.4.1)
rack-cache (1.2)
rack-raw-upload (1.1.1, 1.1.0)
rack-ssl (1.3.2)
rack-test (0.6.2)
rails (3.2.8)
railties (3.2.8)
rake (10.0.3, 10.0.2,
rdoc (3.12)
recaptcha (0.3.4)
responders (0.9.3)
rich (1.4.1, 1.3.1)
rmagick (2.13.1)
rubygems-bundler (1.1.0)
rvm (
sass (3.2.5, 3.2.3, 3.2.1)
sass-rails (3.2.5)
sprockets (2.1.3)
thor (0.16.0)
tilt (1.3.3)
treetop (1.4.12)
tzinfo (0.3.35, 0.3.34)
uglifier (1.3.0)
warden (1.2.1)
whenever (0.8.1)

I tried with bundle update, removing Gemfile.lock and making bundle install, uninstalling all the rake gems (10.0.3 and 10.0.2) and installing the gem with bundle install but nothing worked.

Source: (StackOverflow)

Sidekiq processing with local redis but not with remote

I have a RoR app with background jobs using whenever and sidekiq gems.

In development environment when I launch sidekiq with local redis instance (on localhost) the job keeps getting executed without problems. But when I switch to a remote redis instance (Heroku add-on) and restart sidekiq, it says it started processing, but nothing happens and workers aren't doing any jobs.

Here's my config/schedule.rb (for whenever gem)

every 2.minutes do
  rake "crawler:crawl"

Here's my initializers/redis.rb:

Sidekiq.configure_server do |config|
  config.redis = { :url => 'redis://user:pass@spinyfin.redistogo.com:9098/' }

Sidekiq.configure_client do |config|
  config.redis = { :url => 'redis://user:pass@spinyfin.redistogo.com:9098/' }

If I comment out the content in redis.rb and run a local redis instance, the jobs are processed normally. But when I use this remote redis instance, this shows up and then nothing gets processed:

2013-11-29T15:09:26Z 95156 TID-ov6y7e14o INFO: Booting Sidekiq 2.13.0 using redis://redistogo:user@spinyfin.redistogo.com:9098/ with options {}
2013-11-29T15:09:26Z 95156 INFO: Running in ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin11.4.2]
2013-11-29T15:09:26Z 95156 INFO: See LICENSE and the LGPL-3.0 for licensing details.
2013-11-29T15:09:26Z 95156 INFO: Starting processing, hit Ctrl-C to stop

Source: (StackOverflow)

Issue using whenever gem

I am trying to use whenever gem in my application but I am not able to run rake command in schedule.rb.

Error which is throwing up is

/bin/bash: rake: command not found

I am also using bundler so all my gems are freezed into the application.

Source: (StackOverflow)

Error in bundle when executing cron.job with Whenever

I have the following schedule.rb

every 1.minute do
  runner "User.persist_things"

But looking at my log/cron.log file, the output is the following:

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- bundler/setup (LoadError)
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from /Users/felipeclopes/projects/example/config/boot.rb:6
    from script/rails:5:in `require'
    from script/rails:5

I am using RVM with ruby, the current version is:

$ ruby --version
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin10.8.0]

So it seems that the cron job is loading the incorrect ruby version, but I don't know how to fix this. Can you help me figuring out?

Source: (StackOverflow)

scheduling sequential tasks with whenever gem

i'm using whenever to schedule tasks for a rails application.

I have a task like:

every 24.hours do
   command "do_this"
   rake "do_that"

my point is, when i write it to my crontab, with whenever -w, i see that it generates two independent tasks running at the same time. the problem is, both are logically a sequence, that means, the rake task, "do_that", should run just if the command "do_this" did already, successfully run.

I tried to contact both like command "do_this" && rake "do_that" but i received a syntax error.

  • Does exist any trick to create this dependence between tasks in whenever?

  • Does the crontab execute the jobs at same time, in parallel or it process N tasks scheduled at the same time in a queue?

Source: (StackOverflow)