Rails hourly cron jobs using Whenever fails to run hourly - ruby-on-rails

On Redhat, using Whenever. My cron jobs fail to run hourly. I need help as to why.
Schedule.rb
every 1.hours do
rake "deathburrito:all", :environment => "development"
rake "bamboo:all", :environment => "development"
rake "jira:grab_data", :environment => "development"
end
Crontab -l
0 * * * * /bin/bash -l -c 'cd /var/www/qadashboard && RAILS_ENV=production bundle exec rake deathburrito:all --silent'
0 * * * * /bin/bash -l -c 'cd /var/www/qadashboard && RAILS_ENV=development bundle exec rake bamboo:all --silent'
0 * * * * /bin/bash -l -c 'cd /var/www/qadashboard && RAILS_ENV=development bundle exec rake jira:grab_data --silent'
Can anyone help me? I am not even sure what else I should be checking.

Add
MAILTO=your#email.com
to your crontab. Then enjoy the error reports from cron.
If that won't solve the issue, post the error report here.

bundle will have to be in that subshell's path. Try specifying a full-blown /usr/bin/bundle (or whatever it is).

Append log to config/schedule.rb
set :output, "/var/log/cron"
and create this file 'cron' in /var/log and give it write permission.
Execute
bundle exec whenever --update-crontab
sudo /etc/init.d/cron restart
To see the logs:
tail -f /var/log/cron
will give you more insight on the error.

Related

Rake command works in terminal but not in crontab

I try to execute this crontab line :
*/1 * * * * /bin/bash -l -c 'source $HOME/.bash_profile && cd /home/uservm/RubymineProjects/ && RAILS_ENV=development bundle exec rake accr:generate_pdf'
If I execute cd /home/uservm/RubymineProjects/ && RAILS_ENV=development bundle exec rake accr:generate_pdf in terminal it works perfectly but not in cron
This is because crontab runs in different environment than the normal user(when you type in terminal). So you need to specify your ruby installation path:
Run:
$ which ruby
Copy your ruby path and then, change your crontab to
*/1 * * * * /bin/bash -l -c 'source $HOME/.bash_profile && cd /home/uservm/RubymineProjects/ && /path/to/ruby RAILS_ENV=development bundle exec rake accr:generate_pdf
If this didn't solve then run $ which bundle and replace ruby path with this bundle path and see if it works.
This should solve your problem. For more: See this

Cron jobs with whenever not working in production

I'm trying to implement the gem "whenever" so that I can run automatic tasks every day.
This is my schedule.rb file:
env :PATH, ENV['PATH']
set :output, "log/cron_log.log"
every :day, at: '10:10 am' do
runner "Task.new.get_drivers"
end
every :day, at: '10:15 am' do
runner "Task.new.get_deliveries"
end
every :day, at: '10:20 am' do
runner "Task.new.update_deliveries"
end
In production, I ran this command line "whenever --update-crontab" as explained in the doc.
So far, everything seems to work fine. When I run the command line "whenever", I have this result:
# Begin Whenever generated tasks for: /home/total/site/config/schedule.rb
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/total/.local/bin:/home/total/bin
10 10 * * * /bin/bash -l -c 'cd /home/total/site && bundle exec bin/rails runner -e production '\''Task.new.get_drivers'\'' >> log/cron_log.log 2>&1'
15 10 * * * /bin/bash -l -c 'cd /home/total/site && bundle exec bin/rails runner -e production '\''Task.new.get_deliveries'\'' >> log/cron_log.log 2>&1'
20 10 * * * /bin/bash -l -c 'cd /home/total/site && bundle exec bin/rails runner -e production '\''Task.new.update_deliveries'\'' >> log/cron_log.log 2>&1'
# End Whenever generated tasks for: /home/total/site/config/schedule.rb
But finally, nothing happens (except when I run myself the cron jobs in my console). And my logs display nothing. Have you got any idea of what could be the problem ?
Thank you for your help.
Best regards,
Virginie
Try crontab -l after deploy. This will open your crontab and list all tasks.
Then run whenever --update-crontab and check crontab -l again.
It could be that whenever --update-crontab didn't really update your crontab.
Also try to change :day to 1.day
Also check if your Capfile contains require 'whenever/capistrano', if you deploy via capistrano.

whenever cronjob in Rails app in docker container not working

I am setting a cronjob by whenever inside docker container.
Everything looks fine but it's not work.
Here is part of my code.
Gemfile:
gem 'whenever', :require => false
config/schedule.rb
set :output, "log/cron.log"
env :PATH, ENV['PATH']
set :environment, 'development'
every 1.day, :at => '1:00 am' do
rake "import"
end
every 1.minute do
p "#{Time.now}!"
rake "cron_test"
end
And my dockerfile
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev cron postgresql-client-9.4 dos2unix
RUN touch $APP_HOME/log/cron.log
RUN bundle exec whenever --update-crontab
CMD cron && bundle exec puma
When I run whenever, I get
PATH=/usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
0 1 * * * /bin/bash -l -c 'cd /mlcot && RAILS_ENV=development bundle exec rake import --silent >> log/cron.log 2>&1'
* * * * * /bin/bash -l -c 'cd /mlcot && RAILS_ENV=development bundle exec rake cron_test --silent >> 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.
I make any check I can find from google, but there still nothing happen. I don't even get log/cron.log file.
But if I direct run
/bin/bash -l -c 'cd /mlcot && RAILS_ENV=development bundle exec rake cron_test --silent'
It works. Any suggestion or advice will be appreciate.
I solved it by add this.
Dockfile:
RUN gem install bundler --version 1.11.2
# another line
CMD cron -f && bundle exec puma
And add this line to schedule.rb
ENV.each { |k, v| env(k, v) }
Thanks here and here.

How to set Timezone on whenever gem cronjob

I am using whenever gem + capistrano to automate my cronjob generation whenever I deploy my app to the live server. Currently the cron that being generated by whenever looks like this(without the timezone "TZ") :
30 20 * * * /bin/bash -l -c 'cd /home/deploy/apps/myapp/releases/20160123202716 && RAILS_ENV=production bundle exec rake overdue_payments --silent >> /home/deploy/apps/myapp/releases/20160123202716/log/cron.log 2>&1'
My question is, how do I make whenever generate this line together with the timezone (TZ="Europe/London"), so that it will looks like this:
30 20 * * * TZ="Europe/London" /bin/bash -l -c 'cd /home/deploy/apps/myapp/releases/20160123202716 && RAILS_ENV=production bundle exec rake overdue_payments --silent >> /home/deploy/apps/myapp/releases/20160123202716/log/cron.log 2>&1'
Hope somebody could help..thanks! :)
You can override the default template for jobs like this:
set :job_template, "TZ=\"Europe/London\" bash -l -c ':job'"
Simply define the variable on its own line within your crontab. see man page crontab(5)
TZ=Europe/London
30 20 * * * /bin/bash -l -c 'cd /home/deploy/apps/myapp/releases/20160123202716 && RAILS_ENV=production bundle exec rake overdue_payments --silent >> /home/deploy/apps/myapp/releases/20160123202716/log/cron.log 2>&1'

Rake Cron job error

0/5 * * * * /bin/bash -l -c 'cd /home/mss/ruby/example && RAILS_ENV=development /usr/local/bin/bundle exec rake check_me_out --silent >> /tmp/cron_log.log 2>&1'
Above cron throws an error "bash: bundle: command not found..."
The command works just fine from the command line
Any help will be appreciated.
Okay so I got this working. Cron does not load the profile settings. I had to load the bash_profile as part of the commands and now it works.
0/5 * * * * /bin/bash -l -c 'source ~/.bash_profile && cd /home/mss/ruby/example && RAILS_ENV=development bin/rake check_me_out --silent >> /tmp/cron_log.log 2>&1'
I faced the same problem.
I had solved it when set correct RVM path in CRON:
** * * * /bin/bash -l -c 'cd /home/alex/Projects/my_app && source ~/.bash_profile && rvm use ruby-1.9.3-p194-perf && bundle exec rake RAILS_ENV=development my_tasks --silent >> /tmp/cron_log.log 2>&1'
I also had the same issue when I set up cron on the AWS EC2 server. It was resolved by setting bundle path explicitly in the config/schedule.rb file.
set :bundle_command, "/usr/local/bin/bundle exec"
This creates entry in the cron like:
30 1 * * * /bin/bash -l -c 'cd <app_path> && RAILS_ENV=beta /usr/local/bin/bundle exec rake 'task_name' --silent >> log/cron.log 2>&1'

Resources