Running Ruby on Rails rake tasks from cron
TIP:
This article also appears in our wiki. Help us keep our "Running Ruby on Rails rake tasks from cron" article updated by joining the wiki. Click on the link to get started.
WARNING:
(mt) Media Temple only supports the basic operation and up-time of our (gs) Application Container Technology. Please consult our Scope of Support for further details.
Details:
Rake is a RubyGem. Each (gs) Grid-Service manages it's own set of RubyGems. This is accomplished via a set of environment variables that are initialized every time you log into your (gs) via ssh. They are initialized in the .bash_profile file that is in your home directory. This allows rake to function when you are logged in via ssh.
When rake is run in a cron, the .bash_profile file is not processed, so you must include the environment initialization in your cron script. Here is an example script:
#!/bin/sh
export PATH=$PATH:/home/#####/data/rubygems/bin:/home/#####/data/rubygems/gems/bin
export RUBYLIB=/home/#####/data/rubygems/local/lib/site_ruby/1.8:/home/#####/data/rubygems/lib
export GEM_HOME=/home/#####/data/rubygems/gems
cd $HOME/../../containers/rails/[appname]
echo "Clearing logs..."
rake log:clear
# other rake tasks
You may also use this method to perform mtr tasks. Simply replace the 'rake' command on the last line with mtr. So, instead of 'rake log:clear' you could do 'mtr restart appname'. This will essentially allow you to automate mtr tasks if needed.
NOTE:
This is just an example. The exact lines are found by running the following command when logged into your (gs) via ssh:
cat $HOME/.bash_profile
Just look for the three lines that start with "export".
Revisions:
07-20-2009: Minor Fixes
Fields marked with an asterisk(*) are required. Comment on this article