rake ts:rebuild doesn't work as a crontab task - ruby-on-rails

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.

Related

Blazer gem: Scheduled rake tasks not working automatically but work fine when invoked from console

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?

bundle exec rake doesn't run in cron

I did some researching but all cron and bundle exec related didn't cover the problem I am having so excuse me again if this had been discussed.
I am running Ubuntu 13.10 and have a Ruby On Rails app that have few rake tasks that needs to be run on Cron every few minutes or so.
I run a whenever gem, with the help of which, this syntax
every 3.minutes do
rake 'update_balance'
end
transforms to this line in crontab file
0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent'
And when I copy this line exactly
/bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent'
and run in it console, it runs perfectly fine and updates several records in database, as supposed.
But when set to cron, I can see it running in /var/log/syslog file, but nothing is really executed.
May 13 13:06:01 sandbox2 CRON[9656]: (root) CMD (/bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent')
May 13 13:06:01 sandbox2 CRON[9655]: (CRON) info (No MTA installed, discarding output)
May 13 13:09:01 sandbox2 CRON[9789]: (root) CMD (/bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent')
Even when I add &>/tmp/mycommand.log to crontab command, every next launch of cron command just completely truncates this file, however again, If I launch it manually, it works nicely and leaves me with this output.
2014-05-13T11:11:25Z 10292 TID-2asbo INFO: Sidekiq client with redis options {:url=>"redis://127.0.0.1"}
Sent task for updating 2 users
Any help with this issue is much appreciated.
Thanks.
I've encountered problems like this before due to the fact that cron runs as a different user. With rake in particular, i have to use the full path to rake since the cron user doesn't have the right folder in it's PATH.
So, my cron lines for rake tasks look like this:
30 8 * * 1 cd /ebs/www/apps/myproject/www && /usr/local/bin/rake mailer:send_weekly_expiring_users_reminder RAILS_ENV=production

Ruby on Rails - How to set cron on local

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'

Rake Task from Crontab?

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.

crontab config issues

I would like to setup a crontab for my thinking_sphinx search feature. I am not using any ruby gems for this.
this is my command
*/59 * * * * cd path && rake thinking_sphinx:index RAILS_ENV=production
The syslog gives me this:
CRON[2890]: (user) CMD (cd path && rake thinking_sphinx:index RAILS_ENV=production)
Yet the thinking_sphinx is not getting indexed. It does work if I index it manually.
I added these at the top of the crontab file, now it works.
PATH=/usr/local/bin:/usr/bin:/bin
SHELL=/bin/bash

Resources