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
How to Reference a Function in Ruby
How to Use Bundler with Offline .Gem File
Actionview::Template::Error (Incompatible Character Encodings: Utf-8 and Ascii-8Bit)
Return Index of All Occurrences of a Character in a String in Ruby
Missing Symbol When Installing Ruby-2.3.0 on Os X 10.11.6 by Rvm
Welcome/Home Page in Ruby on Rails - Best Practice
In Ruby's Test::Unit::Testcase, How to Override the Initialize Method
Ruby and "You Must Recompile Ruby with Openssl Support or Change the Sources in Your Gemfile"
How to Uninstall Ruby from /Usr/Local
Iterate Over a Deeply Nested Level of Hashes in Ruby
Ruby on Rails - "Add 'Gem SQLite3'' to Your Gemfile"
Using Ruby Convert Numbers to Words
Is There a Natural_Sort_By Method for Ruby
Why Isn't 'Method=' Treated the Same as Any Other Method
How to Sort a Ruby Hash by Number Value
A Copy of Xxx Has Been Removed from the Module Tree But Is Still Active
Where's the Best Place to Define a Constant in a Ruby on Rails Application