"whenever" gem is generating illegal crontab syntax - ruby-on-rails

I'm using the whenever gem for my crontab scheduling. The syntax being generated is incorrect. Below you will notice it is adding these quotes and forward slashes'\''AddressGeocodeWorker.perform_async'\''
Incorrect crontab
0 4 * * * /bin/bash -l -c 'cd /var/lib/streetcred/agency/releases/streetcred && bin/rails runner -e production '\''AddressGeocodeWorker.perform_async'\'''
Correct crontab would look like this
0 4 * * * /bin/bash -l -c 'cd /var/lib/streetcred/agency/releases/streetcred && bin/rails runner -e production AddressGeocodeWorker.perform_async'
schedule.rb
every 1.day, :at => '4:00 am' do
runner "AddressGeocodeWorker.perform_async"
end
How would I get whenever to generate the correct crontab entry?

Related

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.

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'

Rails hourly cron jobs using Whenever fails to run hourly

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.

cronjob with rails - whenever gem is doing nothing, why? (development mode)

I am trying to auto backup my database every night. I want to use the whenever gem and my application runs still in development environment.
i used wheneverize . command in my app folder to create the config/schedule.rb file, swhcih looks like this:
set :output, "/User/eveadmin/rails/feebacker/log/cron_log.log"
set :environment, "development"
every 1.day, :at => '2:30 am' do
dump_path = "/User/eveadmin/rails/db_backups/dump#{Date.today.to_s}"
command "sudo mysqldump -u user -p secretpasswort feedbacker_development > #{dump_path}.sql"
command "tar -zcvf #{dump_path}.tar.gz #{dump_path}.sql"
command "rm #{dump_path}.sql"
runner "Storage.store_dump '#{dump_path}.tar.gz'"
end
After deploying it to my server, also currently running in development mode, I used the command
whenever --update-crontab feedbacker --set environment=development
After this my crontab looks like the following:
# Begin Whenever generated tasks for: feedbacker
30 2 * * * /bin/bash -l -c 'sudo mysqldump -uroot -prootz feedbacker_development > /User/eveadmin/rails/db_backups/dump2011-07-07.sql >> /User/eveadmin/rails/feebacker/log/cron_log.log 2>&1'
30 2 * * * /bin/bash -l -c 'tar -zcvf /User/eveadmin/rails/db_backups/dump2011-07-07.tar.gz /User/eveadmin/rails/db_backups/dump2011-07-07.sql >> /User/eveadmin/rails/feebacker/log/cron_log.log 2>&1'
30 2 * * * /bin/bash -l -c 'rm /User/eveadmin/rails/db_backups/dump2011-07-07.sql >> /User/eveadmin/rails/feebacker/log/cron_log.log 2>&1'
30 2 * * * /bin/bash -l -c 'cd /Users/thomasmaximini/Sites/rails_projekte/feedbacker && script/rails runner -e development '\''Storage.store_dump '\''\'\'''\''/User/eveadmin/rails/db_backups/dump2011-07-07.tar.gz'\''\'\'''\'''\'' >> /User/eveadmin/rails/feebacker/log/cron_log.log 2>&1'
# End Whenever generated tasks for: feedbacker
It simply does nothing, no cron.log is generated, no backups are done, nothing. So I dont know what I am doing wrong as there are no errors raised to debug. I hope anybody can help, thanks.

Resources