Skip to content

Instantly share code, notes, and snippets.

@yannvery
Created March 24, 2016 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yannvery/f8df11029b580e326b59 to your computer and use it in GitHub Desktop.
Save yannvery/f8df11029b580e326b59 to your computer and use it in GitHub Desktop.
Set a rake task on crontab with rvm

How to set a rake task in crontab with RVM

Introduction

It seems there is multiple ways to configure rvm and use it on a crontab. Here I describe a method which be used on a client app

Let assume you want to launch rake stats on production_app application and write the results on a file

Find the wrapper app

The rake task needs to use the good ruby version. To find it you can explore $rvm_path

$ echo $rvm_path
> /home/deploy/.rvm

$ ls $rvm_path/wrappers
> ruby-2.1.5@production_app  ruby-2.2.2@sweet_app

We get directory wrapper:/home/deploy/.rvm and the wrapper name:ruby-2.1.5@production_app

Set the crontab

The cron job need to do some steps before launch the task:

  • First you need to go into the application directory:/srv/rails_app/production_app
  • Then call the wrapper /home/deploy/.rvm/wrappers/ruby-2.1.5@production_app/rake
  • And finally your task:stats
$ crontab -l
00 16 * * * cd /srv/rails_app/production_app && /home/deploy/.rvm/wrappers/ruby-2.1.5@production_app/rake stats > /home/deploy/production_app_stats.txt --silent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment