whenever: Whenever gem (cron job) not executing rake task, Rails 4
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.
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 1
- Comments: 15 (1 by maintainers)
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.
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'What worked for me here was adding
set :job_template, nilto 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.
@nicolasconnault thanks for the solutions. Works like a charm!