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

rbenv support #486

Closed
ringe opened this issue Sep 9, 2014 · 14 comments
Closed

rbenv support #486

ringe opened this issue Sep 9, 2014 · 14 comments

Comments

@ringe
Copy link

ringe commented Sep 9, 2014

I can't get the whenever bundler combination work in production.

I'm trying to change the cron jobs to include rbenv:
http://snippets.aktagon.com/snippets/601-running-and-debugging-cronjobs-with-rbenv-and-bundler

Is this something to consider?

@ringe
Copy link
Author

ringe commented Sep 9, 2014

@ringe
Copy link
Author

ringe commented Sep 9, 2014

Here's how I got cron jobs working for my Capistrano/rbenv/rails setup:

# Set the path to include rbenv
PATH=$PATH:/home/deploy/.rbenv/shims/:/home/deploy/.rbenv/bin/:/bin:/usr/bin/

# Begin Whenever generated tasks for: /home/deploy/grunndata/current/config/schedule.rb
0 * * * * /bin/bash -l -c '.eval "$(rbenv init -)"; cd /home/deploy/grunndata/current && bundle exec rails runner -e production '\''CacheWorker.perform_async'\'''

I commented out whenever in the Capfile, so I have to add new whenever cron jobs myself, after listing them in my development environment and modifying them to fit the pattern above.

@javan
Copy link
Owner

javan commented Oct 5, 2014

The -l bash option runs an interactive shell, which should execute your startup files. Do you have eval "$(rbenv init -)" in your .bash_profile?

@javan javan closed this as completed Oct 5, 2014
@ringe
Copy link
Author

ringe commented Oct 6, 2014

I have the eval string in my .bashrc and it works fine otherwise.

@javan
Copy link
Owner

javan commented Oct 6, 2014

.bash_profile is executed for login shells and .bashrc is executed for interactive non-login shells. So, you'll need to either add rbenv init to your .bash_profile or modify the job_template in your schedule.rb. The easiest option is to source your .bashrc in your .bash_profile.

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

@fertobar
Copy link

fertobar commented Dec 1, 2015

this post works for me for Ubuntu, on AWS:
http://benscheirman.com/2013/12/using-rbenv-in-cron-jobs.

Eg for a periodical task each 15 minutes:

0,15,30,45 * * * * export PATH=/home/ubuntu/.rbenv/shims:/home/ubuntu/.rbenv/bin:/usr/bin:$PATH; eval "$(rbenv init -)"; cd /var/www/app/current && RAILS_ENV=production bundle exec rake my_task

you can find your rbenv path to replace with 'which rake' command:
eg.

$ which rake
/home/ubuntu/.rbenv/shims/rake

@askrynnikov
Copy link

Ubuntu 16.04, rbenv, Capistrano, whenever, Rails 5

$ which rake
/home/deploy/.rbenv/shims/rake
schedule.rb

job_type :rbenv_rake, %Q{export PATH=/home/deploy/.rbenv/shims:/home/deploy/.rbenv/bin:/usr/bin:$PATH; eval "$(rbenv init -)"; \
                         cd :path && :environment_variable=:environment :bundle_command rake :task --silent :output }

job_type :rbenv_runner, %Q{export PATH=/home/deploy/.rbenv/shims:/home/deploy/.rbenv/bin:/usr/bin:$PATH; eval "$(rbenv init -)"; \
                         cd :path && :bundle_command :runner_command -e :environment ':task' :output }
#... and so on


# example of use
every 60.minutes do
  rbenv_rake "ts:index"
end

@austinarchibald
Copy link

austinarchibald commented Nov 17, 2018

It took me hours to finally find this solution. @askrynnikov's solution did not work for me. I'm also on Ubuntu, rbenv, Capistrano, whenever, and Rails 5.

All I needed to do was add env :PATH, ENV['PATH'] to the top of my schedule.rb.

@gregblass
Copy link

gregblass commented Dec 4, 2019

@austinarchibald's comment worked for me, finally. There should really be a mention of this in the readme for RBENV users.

Add to this the confusion of not really knowing about cron outputs, and it just failing silently...quite the fun 2 hours debugging this.

@arunoday-gloify
Copy link

It took me hours to finally find this solution. @askrynnikov's solution did not work for me. I'm also on Ubuntu, rebenv, Capistrano, whenever, and Rails 5.

All I needed to do was add env :PATH, ENV['PATH'] to the top of my schedule.rb.

This worked for me as well. And just like gregblass, it was quite an experience debugging this! I am not sure if I it is a fundamental misunderstanding of the capistrano rbenv gem or some issue with the gem itself, but I had similar issue with delayed_job, where the bin/delayed_job file just would not get the executable permission when copied to the server by capistrano. So I wrote a task which I had run before invoking the delayed_job:restart task.

@Serg0
Copy link

Serg0 commented Feb 12, 2021

Actually, @javan gave an excellent explanation(#486 (comment)) of what is going on. However, in 2021, on Ubuntu 20.04 I had some mileage variation.

  1. If you do not have .bash_profile it is ok to modify .profile instead.
  2. Sourcing .bashrc for some reason didn't work for me, so I ended up adding those lines from .bashrc:
export PATH="$HOME/.rbenv/bin:$PATH"
export PATH="$HOME/.rbenv/shims:$PATH"
eval "$(rbenv init -)"
export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"

into .profile
But once again - your export path may varies - see your .bashrc.

@brendon
Copy link

brendon commented May 28, 2021

On Ubuntu 20.04 this is at the top of ~/.bashrc:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

You could put eval "$(rbenv init -)" before that part and it'll run in non-interactive mode I'm guessing. I'm trying this now and will report back.

@brendon
Copy link

brendon commented Jun 2, 2021

Just reporting back RE #486 (comment)

It worked well and now the commands run with the correct ruby environment. I'm not sure if it's an ideal setup since I don't like configurations that are order dependent but the default structure of this file gives us no choice.

@roshan92
Copy link

roshan92 commented Apr 8, 2022

On Ubuntu 20.04 this is at the top of ~/.bashrc:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

You could put eval "$(rbenv init -)" before that part and it'll run in non-interactive mode I'm guessing. I'm trying this now and will report back.

I can affirm that this solution works. Thank you @brendon

KevinTriplett added a commit to KevinTriplett/whenever that referenced this issue Aug 6, 2022
Fix for issue "rbenv support javan#486"
javan#486
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants