On server we have the following cronfile:
MAILTO=admin_email#project.com
* * * * * /bin/bash -l -c 'cd /path/to/project/folder;RAILS_ENV=production bundle exec rake some_rake_task'
0 */2 * * * /bin/bash -l -c 'cd /path/to/project/folder;RAILS_ENV=production bundle exec rake another_rake_task'
# and so on
I had no problems with moving it to whenever:
config/schedule.rb:
every '* * * * *' do
rake 'some_rake_task', output: 'log/cron.log'
end
every '0 */2 * * *' do
rake 'another_rake_task', output: 'log/cron.log'
end
# and so on
The question is how do I implement MAIL_TO option in whenever, so that when anything goes wrong admin is getting notified?
Thanks!
If I were you, I would set up error notifications with Airbrake or Rollbar which can both be configured to notify of errors within Rake tasks. That way you'll get notified automatically when something goes wrong in your CRON jobs.
Related
I have set up a couple of rake tasks to run automatically with the whenever gem . The tasks involve running a background Job, performing a database Blazer check and sending failing checks emails.
The last two are provided by the Blazer gem.
This is my schedule.rb file:
set :environment, "development"
every 1.minutes do
rake "health_check:perform"
end
every 1.minutes do
rake 'blazer:run_checks'
end
every 2.minutes do
rake "blazer:send_failing_checks"
end
The "health_check:perform" rake task always performs as expected, and I can see the Job being run in my server log every minute.
The other two, however, don't seem to be executing at all.
I can tell blazer:run_checks is not executing because when a Blazer check is run either manually or automatically, the blazer_checks table last_run_at column value is updated with the timestamp when the last check was run. Also, I can see the update DB transaction in my server log when that happens.
On the other hand "blazer:send_failing_checks" is not working either because well, no emails are ever delivered.
However, if I manually invoke the task from my console via rake "blazer:run_checks" or rake "blazer:send_failing_checks" they perform normally (blazer_checks table is updated and failing check emails are delivered).
I can't spot any error messages in my server log, altough I believe by default rake tasks are set to run in silent mode? Since running whenever -l to list my crontab file returns something like:
* * * * * /bin/bash -l -c 'cd /home/user/code/myapp && RAILS_ENV=development bundle exec rake health_check:perform --silent'
* * * * * /bin/bash -l -c 'cd /home/user/code/myapp && RAILS_ENV=development bundle exec rake blazer:run_checks --silent'
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 /home/user/code/myapp && RAILS_ENV=development bundle exec rake blazer:send_failing_checks --silent'
Any ideas? How can I debug this?
I'm trying to set up whenever gem, but I can't. schedule.rb file:
set :output, "#{path}/log/cron-whenever.log"
every '1 * * * *' do
command "#{path}/sh_files/db_backup.sh"
end
Setting up:
~# whenever --update-crontab everything
[write] crontab file updated
~# crontab -l
# Begin Whenever generated tasks for: everything
1 * * * * /bin/bash -l -c '/some/path/to/files/db_backup.sh >> /some/path/to/files/cron-whenever.log 2>&1'
# End Whenever generated tasks for: everything
And after starting rails server and waiting... nothing happens. No logs update, do db_backup.sh update. Nothing... And I checked manually /some/path/to/files/db_backup.sh command, it works...
It should be schedule.rb instead of scheduler.rb according to the Whenever gem
Suppose, we have a rake task, to be run like,
rake RAILS_ENV=development parse_and_insert_feed_in_db
We can set this task to be execute repeatedly on server using crontab's.
1 0 * * * rake RAILS_ENV=development parse_and_insert_feed_in_db > /some/dir/file.log
How can we set a rake task to be executed on local machine like we set cron on server?
Please try like this:
1 0 * * * /bin/bash -l -c 'cd /home/your/Application/path && rake RAILS_ENV=development parse_and_insert_feed_in_db > /some/dir/file.log'
0 0,2,4,6,8,10,12,14,16,18,20,22 * * * /bin/bash -l -c 'cd /var/www/rails/xxx/releases/20110105175853 && RAILS_ENV=production rake ts:rebuild --silent'
hi, guys. this is a crontab task generated by whenever. it's rebuilding the sphinx index.
it doesn't work when it run as a crontab task, with no error in the /var/log/cron log. but it works when I run the command manually.
anybody can help? thank you very much.
Your cron task looks alittle bizarre to me. Not sure that you want to be calling ts:rebuild all the time, you only need to rebuild if your server gets rebooted, to update the index you just run ts:index, below is the cron task I use for my rails app, it refreshes the sphinx index every 5 minutes.
if your using the user crontab this should work:
*/5 * * * * cd /home/appuser/rails-app; RAILS_ENV=production rake ts:index >> /dev/null
if your putting your crons in /etc/cron.d/ you will need to add the username, like this:
*/5 * * * * appuser cd /home/appuser/rails-app; RAILS_ENV=production rake ts:index >> /dev/null
These settings are for an Ubuntu box, but should work with most linux distros.
Hope this helps.
Stack:
Apache2
Rails 2.3.8
RedHat Linux
Ruby Enterprise 1.8.7
Got the following rake task in my app user's crontab which is meant to pull records into a database table every 15 min:
*/15 * * * * app_user cd /var/www/apps/my_app/current/ && rake thing:do_stuff RAILS_ENV=production
I can see that the cron daemon is running this task in the cron log, but the database table it's supposed to pull records into doesn't change. This task is working without error when I run it manually in the /var/www/apps/my_app/current directory, and pulls records into the table as I expect it to.
I reset the PATH variable in the crontab to reflect using REE, thinking maybe the default path wouldn't jive with /opt/ruby-enterprise...
How do I get this rake task to actually run with cron?
0,15,30,45 * * * * /bin/bash -l -c 'cd /var/www/apps/my_app/current && RAILS_ENV=production bundle exec rake thing:do_stuff --silent'
Try to use full path to rake binary (run in console which rake and replace rake with full path).
For example, if which rake returns the following path:
/Users/bob/.rvm/gems/ruby-1.9.3-p194/bin/rake
You should use the following command to run the rake task:
/Users/bob/.rvm/bin/rvm all do bundle exec rake allocator:snapshot
and I prefer whenever gem for cron jobs in ruby
How to detect if task failed in cron? On fail cron tries to send email. So you can configure postfix to use your smtp settings (from google for example), and add file ~/.forward containing only your email to home directory of user who is running that cronjob in your system.