Use a Cron Job to Automate Sphinx Index Refresh from Rails Rake Task

If using Sphinx, you need to refresh indexes when you add new content to your database. This is fairly easy to do by hand

rake thinking_sphinx:index RAILS_ENV=production

But if you want to automate this and use a cron, remember to set the PATH, SHELL and RAILS_ENV variables for your job. The environment isn’t the same when you’re doing it by hand and your index will fail silently :(

You find your specific PATH and SHELL info like so

echo $PATH
echo $SHELL

To get into the cron and set your schedule

crontab -e
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
SHELL=/bin/bash
RAILS_ENV=production

# re-index production sphinx every 15 minutes
*/15 * * * * root cd /var/www/rails_app && /usr/local/bin/rake thinking_sphinx:index >> /dev/null 2>&1

More information available here

http://heimdull.blogspot.com/2009/05/journey-with-thinking-sphinx-and-crond.html

http://groups.google.com/group/thinking-sphinx/browse_thread/thread/5451458fae7d6124

Related posts:

  1. Set Cron Job to Run Every Five Minutes for a Ruby on Rails Rake Task
  2. Installing Sphinx Search Engine on Mac OS X… or ld: library not found for -lmysqlclient
  3. How to Get Your User’s SHELL and PATH Information
  4. Install Sphinx on Ubuntu
  5. Rake DB Everything, Dump, Destroy, Create, Load
This entry was posted in Ruby on Rails and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Posted June 9, 2010 at 8:11 am | Permalink

    Thank you for this post! I was hitting my head against the wall trying to find out why searchd repeatedly shuts down :) Setting environment vars solved the problem :)

  2. JP
    Posted June 30, 2010 at 7:12 am | Permalink

    OR:

    Call the sphinx indexer directly, also eases the load (you don’t need to fire up rails), eg:

    /usr/local/bin/indexer –config /data/releases/site/current/config/production.sphinx.conf –all –rotate

  3. admin
    Posted June 30, 2010 at 10:39 am | Permalink

    that’s a much better solution. thanks!

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>