whenever icon indicating copy to clipboard operation
whenever copied to clipboard

Whenever tasks not running on Rails 4

Open zsherman opened this issue 10 years ago • 19 comments

Googled around a bunch and couldn't find a good answer for this.

I set up whenever per the docs in my rails 4.1.6 app, and am having it run a simple task in schedule.rb:

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

every 15.minutes do
  runner "Order.sync_orders"
end

Order.rb:

#other crap

def self.sync_orders
    #do stuff
end

Running 'whenever' results in the output:

0,15,30,45 * * * * /bin/bash -l -c 'cd /Users/zach/Apps/myapp && bin/rails runner -e development '\''Order.sync_orders'\'' >> /Users/me/Apps/myapp/log/cron.log 2>&1'

## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options.

The cron.log file is not written to, nor does the method actually get called. The output of crontab -l does indeed list the job there.

Thoughts?

zsherman avatar Oct 26 '14 17:10 zsherman

what is path in "#{path}/log/cron.log"

andywenk avatar Nov 04 '14 16:11 andywenk

What happens if you run the command manually (without logging)?

/bin/bash -l -c 'cd /Users/zach/Apps/myapp && bin/rails runner -e development '\''Order.sync_orders'\'''

javan avatar Nov 09 '14 20:11 javan

I'm having a similar problem, the cron command runs from the command line, but not from the job:

This runs in terminal under user 'deploy':

/bin/bash -l -c 'cd /home/deploy/ebr-dev.delaris.com/releases/20141111223158 && RAILS_ENV=production bundle exec rake openstudio:simulation_update --silent'

No errors in the cron log, but the task is not completed.

On the other hand, specifying the environment as a command lets the task complete:

/bin/bash -l -c 'cd /home/deploy/ebr-dev.delaris.com/current && (RBENV_ROOT=~/.rbenv RBENV_VERSION=2.1.2 RAILS_ENV=production ~/.rbenv/bin/rbenv exec bundle exec rake openstudio:simulation_update)'

from schedule.rb:

every 1.minute do
  #rake "open studio:simulation_update" # <- fails
  command "cd /home/deploy/ebr-dev.delaris.com/current && (RBENV_ROOT=~/.rbenv RBENV_VERSION=2.1.2 RAILS_ENV=production ~/.rbenv/bin/rbenv exec bundle exec rake openstudio:simulation_update)" # <- succeeds
end

ranchhand6 avatar Nov 11 '14 23:11 ranchhand6

Hi All,

i able to run the command below on the terminal and the got the correct result

/bin/bash -l -c 'cd /home/deploy/201411300000 && RAILS_ENV=development bundle exec rake daily_cron:notification_to_redis_update_send_dates --silent'

but when i put it in the schedule.rb, it is not working

every '* * * * *' do
  rake "daily_cron:notification_to_redis_update_send_dates"
end

Kindly advise.

estelleccl avatar Dec 02 '14 08:12 estelleccl

I got the same problem, you have to specify the 'rake' path to make it work. EX

* * * * * /bin/bash -l -c 'cd /my/app/path && RAILS_ENV=development bundle exec /my/fantastic/rake/path/rake :task --silent'

In schedule.rb

job_type :rake,    "cd :path && :environment_variable=:environment bundle exec /my/fantastic/rake/path/rake  :task --silent :output"
``

Fnux avatar Dec 27 '14 12:12 Fnux

I've been in a circular loop on this...

  1. I got the bundle command not found error.
  2. Added env :PATH, ENV['PATH'] to my schedule.rb -- did not fix the issue
  3. Created some test commands in my schedule.rb e.g. command 'cd /rails_apps/myapp && RAILS_ENV=production bundle exec rake -T' -- it worked!
  4. Everything else started working!
  5. Removed the test command sfrom my schedule.rb, back to my original version...
  6. Now it works.

The only difference is that in my production schedule.rb, my task is set to run every 1.day, :at=>'1:00am', and in my test version, my task was set to run every 1.minute

vanboom avatar Dec 30 '14 23:12 vanboom

I followed what was here and it worked. in whenever --update-cron theNameOfCronJob I omitted theNameOfCronJob

soheildanesh avatar Jan 09 '15 20:01 soheildanesh

I had this same problem with Rails 4 + Capistranov3 + whenever. This fixed it; with reference to this and this do this

In Rails 4, your app's bin/ directory contains executables that are versioned like any other source code, rather than stubs that are generated on demand.

Here's how to upgrade:

bundle config --delete bin # Turn off Bundler's stub generator rake rails:update:bin # Use the new Rails 4 executables git add bin # Add bin/ to source control

Problem is that capistrano-bundler overwrites the bin directory. In deploy.rb remove bin from set :linked_dirs Hope that helps

rgratwick avatar Apr 02 '15 02:04 rgratwick

@soheildanesh This worked, Thanks!

IvRRimum avatar Jun 25 '15 21:06 IvRRimum

@javan @zsherman finally, how did you solve your problem? /bin/bash -l -c 'cd /home/deploy/weishop_deploy/current && bin/rails runner -e production '''Spree::Order.order_flow'''', it works at terminal, but it's running at cron.

  1 # Begin Whenever generated tasks for: order_flow
  2 * * * * * /bin/bash -l -c 'cd /home/deploy/weishop_deploy/current && bin/rails runner -e production '\''Spree::Order.order_flow'\'''
  3
  4 # End Whenever generated tasks for: order_flow

seaify avatar Jul 20 '15 01:07 seaify

In addition to defining the full path to Rake I also needed to define the full path to Bundle in order for it to work. For me the cron job is run as root so what you can try is to run the command as root to see if you get any error messages.

ohamren avatar Jan 12 '16 12:01 ohamren

I am not able to get any issue in this locally.

SwapnilChincholkar: example (master) $ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin13]
SwapnilChincholkar: example (master) $rails -v
Rails 4.2.1
SwapnilChincholkar: example (master) $whenever -v
Whenever v0.9.7

My schedule.rb is

set :output, "/logs/cron_log.log"
every 2.minutes do  
  rake "test_cron:check_task"
end

Rake task is

SwapnilChincholkar: example (master) $cat lib/tasks/test_cron.rake 
namespace :test_cron do
  desc "checking tasks scheduling"
  task :check_task => :environment do
    puts "Inside task now #{Time.now}"
  end
end

Log file tail is

SwapnilChincholkar: example (master) $tail -f /log/cron_log.log 
Inside task now 2016-06-22 13:16:05 +0530
Inside task now 2016-06-22 13:18:05 +0530
Inside task now 2016-06-22 13:20:05 +0530
Inside task now 2016-06-22 13:22:07 +0530
Inside task now 2016-06-22 13:24:08 +0530
Inside task now 2016-06-22 13:28:36 +0530
Inside task now 2016-06-22 13:30:05 +0530
Inside task now 2016-06-22 13:32:06 +0530

Cron created is

SwapnilChincholkar: example (master) $crontab -l

# Begin Whenever generated tasks for: /Users/SwapnilChincholkar/work/self_work/example/config/schedule.rb
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /bin/bash -l -c 'cd /Users/SwapnilChincholkar/work/self_work/example && RAILS_ENV=production bundle exec rake test_cron:check_task --silent >> /log/cron_log.log 2>&1'

# End Whenever generated tasks for: /Users/SwapnilChincholkar/work/self_work/example/config/schedule.rb

swapnilchincholkar avatar Jun 22 '16 08:06 swapnilchincholkar

I got same issue that task does not work with cron. same as others, specifying absolute path to take solved it. Still I don't know how to do it with schedule.rb seems to be rbenv problem for me.


Finally, found that people create rbenv_rake to handle this issue.

https://github.com/javan/whenever/pull/521

toshitanian avatar Aug 30 '16 12:08 toshitanian

You should have ran whenever --update-crontab, this would start your scheduled task, you have set the output to "#{path}/log/cron.log", then go and look in that file

Teebo avatar Sep 06 '16 07:09 Teebo

Ubuntu 16.04, rbenv, Capistrano, whenever, Rails 5

$ which rake
/home/deploy/.rbenv/shims/rake
schedule.rb

job_type :rbenv_rake, %Q{export PATH=/home/deploy/.rbenv/shims:/home/deploy/.rbenv/bin:/usr/bin:$PATH; eval "$(rbenv init -)"; \
                         cd :path && :environment_variable=:environment :bundle_command rake :task --silent :output }

job_type :rbenv_runner, %Q{export PATH=/home/deploy/.rbenv/shims:/home/deploy/.rbenv/bin:/usr/bin:$PATH; eval "$(rbenv init -)"; \
                         cd :path && :bundle_command :runner_command -e :environment ':task' :output }
#... and so on


# example of use
every 60.minutes do
  rbenv_rake "ts:index"
end

askrynnikov avatar Apr 19 '17 20:04 askrynnikov

Note askrynnikov's solution also works on Ubuntu 14.04, rbenv, Capistrano, whenever, Rails 4.2.x

dvodvo avatar Jan 21 '18 22:01 dvodvo

This took me HOURS to figure out. Everyone was talking about rbenv being simplistic and better than rvm, and it caused me so many issues.

@askrynnikov's solution did not work alone. I had to add env :PATH, ENV['PATH'] to top of my schedule.rb. I don't know if removing @askrynnikov's lines will impact it, but I'm just happy it's finally WORKING. Goodness me. I'll play around with it later.

austinarchibald avatar Nov 17 '18 02:11 austinarchibald

Can we close this since it's stale and no interaction with has been made in years ? @benlangfeld

bragamat avatar Nov 12 '23 23:11 bragamat

Definitely!

dvodvo avatar Nov 13 '23 06:11 dvodvo