I'm trying to run a Ruby script from cron.
The script uses Bundler to manage gem dependencies. Since cron does not run in $PWD
I get a 'Could not locate Gemfile' error from Bundler everytime, which makes sense since Gemfile is not in the currrent path when running from cron.
Is there a way to tell Bundler to use a Gemfile not in the current path?
The Gemfile is wherever you want it to be - usually in the main directory of your project and the name of the file is Gemfile . It's convenient to have one because it allows you to use Bundler to manage which gems and which versions of each your project needs to run.
Gemfile. A Gemfile is a list of gems used by your site. Every Jekyll site has a Gemfile in the main folder.
I know that when using gem install , the gem will be stored under /home/username/. rvm/gems/, under which gemset the gem was installed.
The best thing to do would be to cd
into the directory in question in your cron. You could also use the BUNDLE_GEMFILE
environment variable to point at the Gemfile. Please let us know if you have any problems with BUNDLE_GEMFILE
.
You could have the main script change its dir:
Dir.chdir File.dirname(__FILE__)
You might have to tweak it using File.expand_path
to get your app's root dir (where the Gemfile
is). Suppose your script is /apps/myapp/bin/main.rb
; the chdir
line would be:
Dir.chdir File.expand_path('../..', __FILE__)
Of course, you have to make sure your app doesn't break because of the chdir - but it shouldn't if you're cd
-ing in the cron.
If the script is a daemon and you want it to be changed to the root dir /
during normal operation, you can do that after calling Bundler.setup
or Bundler.require
.
While you could do cd
in the cron, I prefer not to, because crons are already hard enough to read and maintain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With