whenever icon indicating copy to clipboard operation
whenever copied to clipboard

Whenever gem (cron job) not executing rake task, Rails 4

Open UsmanMalik opened this issue 8 years ago • 15 comments

I am trying to run rake task using whenever gem, did everything what was required but fro some reason cant execute the task successfully.

schedule.rb

set :environment, "development"
set :output, {:error => "log/cron_error_log.log", :standard => "log/cron_log.log"}

every 1.minute do
  rake "notify:user"
end

notify_user.rake

namespace :notify do
  desc "Rake task to push divisions"
  task :user => :environment do
    t = Division.trigger_divisions
    puts "#{Time.now} - Success!" + t.to_s
  end
end

I did whenever --update-crontab and also crontab -l

# Begin Whenever generated tasks for: /home/usman/projects/Saloon/config/schedule.rb
* * * * * /bin/bash -l -c 'cd /home/usman/projects/Saloon && RAILS_ENV=development bundle exec rake notify:user --silent >> log/cron_log.log 2>> log/cron_error_log.log'

# End Whenever generated tasks for: /home/usman/projects/Saloon/config/schedule.rb

# Begin Whenever generated tasks for: store

# End Whenever generated tasks for: store

Log are showing grep CRON /var/log/syslog

Oct  9 15:56:01 usman-Inspiron-3521 CRON[4430]: (usman) CMD (/bin/bash -l -c 'cd /home/usman/projects/Saloon && RAILS_ENV=development bundle exec rake notify:user --silent >> log/cron_log.log 2>> log/cron_error_log.log')
Oct  9 15:57:01 usman-Inspiron-3521 CRON[4453]: (usman) CMD (/bin/bash -l -c 'cd /home/usman/projects/Saloon && RAILS_ENV=development bundle exec rake notify:user --silent >> log/cron_log.log 2>> log/cron_error_log.log')
Oct  9 15:58:01 usman-Inspiron-3521 CRON[4461]: (usman) CMD (/bin/bash -l -c 'cd /home/usman/projects/Saloon && RAILS_ENV=development bundle exec rake notify:user --silent >> log/cron_log.log 2>> log/cron_error_log.log')

Not sure what i am doing wrong here. If i run

cd /home/usman/projects/Saloon && RAILS_ENV=development bundle exec rake notify:user task executes successfully.

UsmanMalik avatar Oct 09 '16 11:10 UsmanMalik

Also, i am using ubuntu 14.04

UsmanMalik avatar Oct 09 '16 12:10 UsmanMalik

You havn't included any description of what you believe to be wrong. Is there no output in your specified log files?

benlangfeld avatar Oct 09 '16 12:10 benlangfeld

Hi Ben, Thanks for the reply. Anything i am trying to execute is not getting executed form the logs i can see that ( logs mentioned above) it is trying to execute but getting failed. Not sure what the reason is. From the example above (rake task) output should be the GCM push trigger (which works if i run the command manually)

UsmanMalik avatar Oct 09 '16 18:10 UsmanMalik

Hi Ben, sorry for disturbing you. Is there is any chance you can help. Thanks.

UsmanMalik avatar Oct 11 '16 22:10 UsmanMalik

@UsmanMalik Are the log files present? Have you been able to figure out the solution?

prasadsurase avatar Oct 25 '16 17:10 prasadsurase

Hi, I'm having the same problem I think: created a simple rake task, and added in the schedule.rb set :output, "/log/cron_log.log" and every 5.minutes do rake "rake:rask" end... However nothing happens :/

I checked executing directly the cron generated task, which works perfectly. Without any log, I can't understand why we are having this problem. Btw, I'm using Ubuntu 16.04.

Thx in advance for any help!

PS: After completing my schedule.rb I ran whenever --update-crontab myappname --set environment=development... If it does matter.

ghost avatar Jan 13 '17 16:01 ghost

Try this: crontab -l | sed 's|bundle|bin/bundle|g' | crontab -

rdpse avatar Mar 21 '17 16:03 rdpse

Same proble occured to me. I am using whenever (0.9.7)

I think option 1.minute is not working properly.

every 1.minute do
    rake "application:capture_price"
end

this generate

* * * * * /bin/bash -l -c 'cd /directory && RAILS_ENV=development bundle exec rake application:capture_price --silent >> log/cron_log.log 2>> log/cron_error_log.log'

No numbers are generated in this case. only five asterisks.

wheareas

every 2.minutes do
  rake "application:capture_price"
end

generates

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 /directory && RAILS_ENV=development bundle exec rake application:capture_price --silent >> log/cron_log.log 2>> log/cron_error_log.log'

imran3180 avatar May 31 '17 03:05 imran3180

I think option 1.minute is not working properly.

Cronjobs have a granularity of 1 minute, so five asterisks means "run every minute." The output is correct in this case.

The output for 2 minutes is correct, though definitely too verbose. It could simply be written as */2 * * * * /bin/bash -l -c 'cd /directory && RAILS_ENV=development bundle exec rake application:capture_price --silent >> log/cron_log.log 2>> log/cron_error_log.log'

rdpse avatar May 31 '17 18:05 rdpse

Having the same problem.

radvc avatar Jul 06 '17 12:07 radvc

Hi all, I was having the same issue, and found out that the system wasn't finding the "bundle" command. The solution was to add

set :output, "log/cron_log.log" env :PATH, ENV['PATH']

to config/schedule.rb

The first line lets you debug any further whenever/cron issues by adding a log file to your production app. The second line ensures that the bundle command is available.

nicolasconnault avatar Aug 01 '17 03:08 nicolasconnault

@nicolasconnault thanks for the solutions. Works like a charm!

ChielHackman avatar Aug 08 '17 10:08 ChielHackman

What worked for me here was adding

set :job_template, nil

to config/schedule.rb This might lead to different issues though since the readme states that running the command through bash is needed for RVM to work, so YMMV.

SimonThordal avatar Apr 02 '18 18:04 SimonThordal

@nicolasconnault thank u so much!!! works fine :)

laurajaime avatar Feb 28 '23 08:02 laurajaime

I believe we can close this issue given that it has had no interaction in years and might be stale by now.

cc @benlangfeld

bragamat avatar Nov 13 '23 00:11 bragamat