1

I'm trying to run rake task with cron on Ubuntu with rvm

My crontab -l

* * * * * cd /media/sf_visa-tracker/ && /home/ruslan/.rvm/gems/ruby-2.1.0/bin/rake parse RAILS_ENV=production >> /var/log/visa-parse.log 2>&1

which rake says

/home/ruslan/.rvm/gems/ruby-2.1.0/bin/rake

And I keep getting this error in my log (/var/log/visa-parse.log)

/usr/bin/env: ruby_executable_hooks: No such file or directory

Also, if I run cd /media/sf_visa-tracker/ && /home/ruslan/.rvm/gems/ruby-2.1.0/bin/rake parse RAILS_ENV=production from my terminal, rake task works.

Pls help ;)

Ruslan Savenok
  • 153
  • 2
  • 17

2 Answers2

3

The simplest way to use rvm with cron is to use rvm's wrappers. Your shell sets up a whole bunch of rvm-related environment when it starts, and that's missing from your cron job. The wrappers are versions of the Ruby-related commands that take care of this for you.

In this case, if you have rvm installed to /usr/local, your cron job should look something like this:

* * * * * cd /media/sf_visa-tracker/ && /usr/local/rvm/wrappers/ruby-2.1.0/bin/rake parse RAILS_ENV=production >> /var/log/visa-parse.log 2>&1

You could also bundle up your rvm setup into a shell script that loads rvm before invoking rake; there's more details of both approaches in rvm's documentation on working with cron.

Alex P
  • 5,942
  • 2
  • 23
  • 30
  • in order to get rake executable path - use "which rake" to get rake path and then substitute "bin" with "wrappers" - check https://stackoverflow.com/questions/26247926/how-to-solve-usr-bin-env-ruby-executable-hooks-no-such-file-or-directory – chetang Jun 13 '17 at 12:33
0

I struggled to get something that worked and seemed clean. Here's what I ended up doing in case it helps.

crontab 0 5 * * * /home/deploy/apps/myproject/shared/cron/daily_updates.sh

I put the script in myproject/shared since I'm using Capistrano and that seemed like a good place for things that would persist between releases.

shared/cron/daily_updates.sh

#!/usr/bin/env bash

# You will need to update this as you bump ruby versions
source /home/deploy/.rvm/environments/ruby-2.3.1@myproject

# Since I had a separate place for Rails specific environment variables (e.g. secrets, app keys)
# you should set the permissions on that file to be owner read/write only
source /home/deploy/.rails_env

cd /home/deploy/apps/myproject/current
rake RAILS_ENV=production daily_update:send_updates 2>&1
Steven Chanin
  • 570
  • 5
  • 5