Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using RVM with Cron #4293

Open
Wardrop opened this issue Jan 29, 2018 · 5 comments
Open

Using RVM with Cron #4293

Wardrop opened this issue Jan 29, 2018 · 5 comments

Comments

@Wardrop
Copy link

Wardrop commented Jan 29, 2018

I have a bash script I'm calling as a cron job. This bash script calls multiple ruby scripts. I'm struggling to find the best method of configuring the appropriate ruby version to use. I want to avoid editing the bash script or ruby scripts, as these should be portable and shouldn't contain hardcoded environment-specific stuff.

Here's an example of what I'm trying to do. In this scenario, my RVM default ruby version is RVM is 2.3. My ruby script, titled my_ruby_script.rb below, requires ruby 2.5. How do I achieve this?

/etc/cron.d/my_bash_script

SHELL=/usr/local/rvm/bin/rvm-shell
* * * * * root /opt/project/my_bash_script_.sh

/opt/project/my_bash_script.sh_

#!/usr/bin/env bash
/opt/project/my_ruby_script.rb

/opt/project/my_ruby_script.rb

#!/usr/bin/env ruby
puts "Hello, I need ruby 2.5.0"

I've tried a few things, including doing the following in my cron file, but I get "RVM is not a function ...".

SHELL=/usr/local/rvm/bin/rvm-shell
* * * * * root rvm use ruby-2.5 && /opt/project/my_bash_script_.sh

I've also tried creating a .ruby-version file, and cd'ing into the directory like so, but it's not respecting what's in .ruby-version, and still uses the RVM default ruby.

SHELL=/usr/local/rvm/bin/rvm-shell
* * * * * root cd /opt/project/ && ./my_bash_script_.sh

None of the RVM documentation has managed to help me. All the cron-related examples it gives call a ruby script directly which is much more trivial. Any advice?

@Wardrop
Copy link
Author

Wardrop commented Jan 29, 2018

Ok, I found the following which seems to work:

SHELL=/usr/local/rvm/bin/rvm-shell
* * * * * source "/usr/local/rvm/scripts/rvm" && rvm use ruby-2.5 &&  /opt/project/my_bash_script_.sh

Shouldn't rvm-shell be sourcing that for me? Is that the best or "most correct" way to achieve this?

@mpapis
Copy link
Member

mpapis commented Feb 6, 2018

you could use .ruby-version with /usr/local/rvm/bin/rvm /opt/project/ do ./my_bash_script_.sh or specify the version like this /usr/local/rvm/bin/rvm 2.5.0 do /opt/project/my_bash_script_.sh

@duffyjp
Copy link

duffyjp commented Jun 13, 2018

@Wardrop I've been doing my cron like this for years:

🍕 $ crontab -l

* * * * * /bin/bash -l -c 'ruby /Users/duffyjp/scripts/log-cpu.rb' &>/tmp/cpu_log.txt

I like doing it this way as you don't have to worry about pulling the rug out from other scheduled tasks by changing the SHELL for everything.

@Tuman829
Copy link

rvm cron setup should add the variables to your crontab

@sroller
Copy link

sroller commented Aug 4, 2021

rvm cron setup

hangs on my system :-(. I have do Ctrl-Z + kill %1
not even Ctrl-C stops it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants