How to Run a Ruby Script Using Rbenv with Cron

How to run a Ruby script using rbenv with cron

I've found a solution to load rbenv. Either with a loader importing rbenv to the PATH :

*/1 * * * * /bin/bash -c '. $HOME/.rbenv/loader.sh ; cd /data/app/; ruby -v'

The '.' before '$HOME/.rbenv/loader.sh' is important, it runs the script in the current shell

Or without loader, which is better :

*/1 * * * * /bin/bash -c 'export PATH="$HOME/.rbenv/bin:$PATH" ; eval "$(rbenv init -)"; cd /data/app/; ruby -v'

.desktop - run RBENV shell - Run app with ruby version

For the details of setting up a .desktop file, please see: Creating a .desktop file for a new application.

Since rbenv (when installed properly) is a function and not an executable, it won't be as simple as pointing to the right file. One solution would be to create a shell script that sets up the environment:

#!/usr/bin/env bash    # Execute as a bash script
eval "$(rbenv init -)" # Initialize rbenv
ruby $* # Run the ruby script specified the .desktop file

Then make sure the script is executable and put it in your .desktop Exec key:

Exec=/path/to/script/ruby_stub.sh script_you_want_to_run.rb ARGS

crontask generate by Whenever doesn't run

try run the task not silently but with output-forwarding to a file:

RAILS_ENV=staging bundle exec nohup cinemas:import >/tmp/log.txt 2>&1

so for cron the rvm has special wrappers, which should be run to setup proper environment, like follows:

* * * * * cd folder; RAILS_ENV=staging /usr/local/rvm/wrappers/ruby-2.1.1@projectX/bundle exec nohup cinemas:import >/tmp/log.txt 2>&1

if you are using the rbenv, the script runner will be seen as follows:

* * * * * /bin/bash -c '. $HOME/.rbenv/loader.sh; cd folder ; RAILS_ENV=staging bundle exec cinemas:import >/tmp/log.txt 2>&1'


Related Topics



Leave a reply



Submit