Cron tabs cannot find proper Ruby version via Whenever gem - ruby-on-rails

I wanted to set a cron tab which would execute a task located in my Rails app. In my config/schedule.rb:
every 2.minutes do
#do something
end
Despite putting this task successfully into my cron jobs list it never gets executed. When I try to manually launch the script saved in my cron jobs list, that is:
/bin/bash -l -c 'cd /Users/me/Desktop/railsapp && RAILS_ENV=production bundle exec rake reports:fetch --silent'
I get error about wrong Ruby version:
Your Ruby version is 2.6.3, but your Gemfile specified 2.6.6
I use rbenv and both my local and global versions are set to 2.6.6. Furthermore version 2.6.3 is not even present on the list of available ones.
How can I change this version to proper one and make my cron jobs executable?

I am getting same errors on Pro M1.
My app ruby version is 3.1.2, default MAC system verison is 2.6.8
I have add to ~/.bash_profile file this content:
export RBENV_ROOT="${HOME}/.rbenv"
if [ -d "${RBENV_ROOT}" ]; then
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init -)"
fi
And then run . ~/.bash_profile.
Add this line to schedule.rb: env :PATH, ENV["PATH"]
=> Don't forget update crontab with: whenever --update-crontab --set environment='development'
Crontab is working with rbenv.
Refs: https://blog.eq8.eu/article/cronntab-rbenv-bundle-exec-rake-task.html

Related

Cronjobs for Rails stopped working after switching to asdf

I recently changed from rvm to asdf.
Since then, I can't get cronjobs to run.
crontab -l
* * * * * /bin/bash -l -c 'cd /var/www/jobs/code && RAILS_ENV=production bundle exec rake cron_test' >> /tmp/cron_test_output 2>&1 /tmp/cron_error
gives me /tmp/cron_error: bundle: command not found. There is no output to stderr.
gem install bundler has not worked for the jobs user.
Not sure where to install bundler to make this work.
The rake tasks work when run independently. The cronjobs that are not rake tasks run. Cron is working.
Edit:
While the cronjobs were working before, they obviously can't now find the location of bundler. By changing bundle exec to the full path /home/jobs/.asdf/shims/bundler exec, I have managed to get this to work.
I have been using the whenever gem to create cron jobs for rake tasks. Turns out the easiest way to get around this problem is to make sure that the environment path is included in the whenever generated crontab.
within config/schedule.rb for the whenever gem, I added the following:
env :PATH, ENV['PATH']
and now everything is working as the cron job can now find the bundler.
Based on this issue from the whenever gem.
And this one too.

Bundler Trying To Run Under Wrong Version of Ruby

I have a rails site, which I deploy via ssh using a git post-receive hook. When I ssh into the server and run bundle install it runs correctly under the specified ruby version of 2.2.2. However, when I push to the server from my local machine and it hits the 'bundle install command', I get the following:
hooks/post-receive: /usr/local/bin/bundle: /usr/bin/ruby1.9.1: bad interpreter: No such file or directory
I can't find for the life of me why it is pointing to ruby1.9.1. This directory does not exist. I do see a directory for ruby2.3 in that directory, but not ruby2.2.2 which is the correct directory. Something is quite fouled up, but I can't figure how to fix it. Anyone seen anything like this?
UPDATE: Here is my post-receive hook, as per the request below...
#!/bin/bash
GIT_DIR=/home/deploy/www_production
WORK_TREE=/home/deploy/www
export MGOTS_DATABASE_USER='user'
export MGOTS_DATABASE_PASSWORD='pass'
export RAILS_ENV="production"
. ~/.bash_profile
while read oldrev newrev ref
do
if [[ $ref = refs/heads/master ]];
then
echo "Master ref received. Deploying master branch to production..."
mkdir -p $WORK_TREE
git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f
mkdir -p $WORK_TREE/shared/pids $WORK_TREE/shared/sockets $WORK_TREE/shared/log
# start deploy tasks
cd $WORK_TREE
bundle install
rake db:create
rake db:migrate
rake assets:precompile
rake requests:cleanup
sudo restart puma-manager
sudo service nginx restart
# end deploy tasks
echo "Git hooks deploy complete"
else
echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
fi
done
UPDATE: For the sake of clarity, as the answer points to the correct place to find the answer, but doesn't state it exactly, I am posting my updated hook file here. You can see the difference between this one and the one above, and that is what solved the problem. Please note that the path to the rvm directory can be found by typing the command: which rvm - that's the one you want to point to.
#!/bin/bash
GIT_DIR=/home/deploy/www_production
WORK_TREE=/home/deploy/www
export MGOTS_DATABASE_USER='user'
export MGOTS_DATABASE_PASSWORD='pass'
export RAILS_ENV="production"
export RUBYGEMS_GEMDEPS="/home/deploy/.rvm/ruby-2.2.2#www/gems"
. ~/.bash_profile
[[ -s "/usr/share/rvm/bin/rvm" ]] && source "/usr/share/rvm/bin/rvm"
while read oldrev newrev ref
do
if [[ $ref = refs/heads/master ]];
then
echo "Master ref received. Deploying master branch to production..."
mkdir -p $WORK_TREE
git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f
mkdir -p $WORK_TREE/shared/pids $WORK_TREE/shared/sockets $WORK_TREE/shared/log
# start deploy tasks
cd $WORK_TREE
bundle install
rake db:create
rake db:migrate
rake assets:precompile
rake requests:cleanup
sudo restart puma-manager
sudo service nginx restart
# end deploy tasks
echo "Git hooks deploy complete"
else
echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
fi
done
You need to load RVM functions to the shell script. link
Or just switch to Rbenv :)
First, set your default ruby to use the version 2.2.2
Are you using RVM? For RVM its: rvm use --default 2.2.2

Cron + whenever not working with RVM

I am using RVM environment. RUby version : 2.1.2 rails : 4.1.1
schedule.rb :
every 1.minute do
runner "note.send_mail"
end
I used whenever --update-crontab to update the cron tab.
when I check the jobs using crontab -l it shows up as below with no proper time set up.
and the cron job does not work.
* * * * * /bin/bash -l -c 'cd /Desktop/folder1/blog2 && bin/rails runner -e production '\''note.send_mail'\'''
Can some one help me out fix this. Thanks!
Go to your ~/.rvmrc file and add the following:
rvm_trust_rvmrcs_flag=1
Then whenever --update-crontab again. According to the README of whenever:
If your production environment uses RVM (Ruby Version Manager) you will run into a gotcha that causes your cron jobs to hang. This is not directly related to Whenever, and can be tricky to debug. Your .rvmrc files must be trusted or else the cron jobs will hang waiting for the file to be trusted. A solution is to disable the prompt by adding this line to your user rvm file in ~/.rvmrc
rvm_trust_rvmrcs_flag=1
This tells rvm to trust all rvmrc files.
If that doesn't work for you, try other solutions mentioned in this page: RVM-Notes.
You could define a custom runner that loads rvm on the command line, like
job_type :runner_with_rvm, 'source /etc/profile.d/rvm.sh; cd :path;rvm 2.0#gemset do bundle exec script/rails runner -e :environment ':task' :output'
every 1.minute do
runner_with_rvm "Note.send_email"
end
Replace 2.0#gemset with your desired ruby version and gemset.
Could be that /etc/profile.d/rvm.sh is something else in your environment too.

Turn off rvm greeting on login

I have problem with foreman and rvm - foreman generates start and stop init.d scripts for resque worker.
The problem is that start script tries to write pid of new worker in file, but writes it with rvm message Using /home/dev/.rvm/gems/ruby-2.0.0-p247 that appears on login.
Of course, I may edit generated script to cut off that part, but then I'll need to do that every time scripts are regenerated.
Here is how foreman tries to save pid:
su - $USERNAME -c 'cd /home/path/to/current; export PORT=5000; bundle exec rake environment resque:work QUEUE=* RAILS_ENV=production >> /var/log/app_name/job-1.log 2>&1 & echo $!' > $PIDDIR/job.1.pid
How can I disable that rvm message? Or any other solutions?
you are using cd to load ruby, the Using message is most likely coming from it, the ad hook solution would be to silence it:
cd /home/path/to/current >/dev/null
there is also a flag to silence the message, but it might not work with older .rvmrc files:
echo rvm_use_flag=0 >> ~/.rvmrc

Rails cron whenever, bundle: command not found

I am trying to use whenever to execute a rake task onces a day. Im getting this error
/bin/bash: bundle: command not found
/home/app/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Could not find bundler (>= 0) amongst [minitest-1.6.0, rake-0.8.7, rdoc-2.5.8] (Gem::LoadError)
from /home/app/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:256:in `to_spec'
from /home/app/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems.rb:1210:in `gem'
from /home/app/.rvm/gems/ruby-1.9.2-p180/bin/bundle:18:in `<main>'
Here is my crontab
# Begin Whenever generated tasks for: /home/af/www/app/releases/20120216172204/config/schedule.rb
PATH=/home/af/.rvm/gems/ruby-1.9.2-p180#global/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
0 0 * * * /bin/bash -l -c 'cd /home/af/www/app/releases/20120216172204 && rvm 1.9.1-p180; RAILS_ENV=production /home/af/.rvm/gems/ruby-1.9.2-p180/bin/bundle exec rake daily:stats --silent >> /home/af/www/app/releases/20120216172204/log/cron.log 2>&1'
# End Whenever generated tasks for: /home/af/www/app/releases/20120216172204/config/schedule.rb
I'm at a loss as to why it isn't working. If I run the command:
cd /home/af/www/app/releases/20120216172204 && rvm 1.9.1-p180; RAILS_ENV=production /home/af/.rvm/gems/ruby-1.9.2-p180/bin/bundle exec rake daily:stats --silent >> /home/af/www/app/releases/20120216172204/log/cron.log 2>&1
It works fine, not sure whats going on here.
You can also ensure your PATH ends up in the crontab, by putting the following at the top of the schedule.rb file:
env :PATH, ENV['PATH']
https://groups.google.com/forum/#!msg/whenever-gem/yRLt3f2jrfU/Exu3xfCo8DAJ
If above solution don't work for you, try:
env :GEM_PATH, ENV['GEM_PATH']
In my case I just ran :
rvm env --path -- ruby-version[#gemset-name]
Referring to cron job setup doc
Added new source line to the command for ruby path
before bundle command in the crontab -e
source /usr/local/rvm/environments/ruby-1.9.3-p392;
Now the commands like as below:
Before:
0 4 * * * cd /home/current && bundle exec rake my_rake RAILS_ENV=production
After:
0 4 * * * cd /home/current && source /usr/local/rvm/environments/ruby-1.9.3-p392; bundle exec rake my_rake RAILS_ENV=production
Cheers!!!
After so many try outs the following seems to work
Type the following from terminal
Type crontab -e
This opens the crontab for editing. You will see two lines as below:
# cron clears out environment variables, but Rubber.root/script/rubber uses
# "rvm do default" to run, so no longer any need to setup ruby env vars here,
# all we need is PATH
PATH=/<path to bundle>/bundle/ruby/1.9.1/bin:/usr/local/rvm/gems
AND
# Begin Whenever generated tasks for: /mnt/wamjoke-production/releases/20120912$
PATH=/<path to bundle>/shared/bundle/ruby/1.9.1/bin:/usr/local/rvm/gems
Comment out both lines beginning with PATH.
Do the above step whenever you run "bundle exec whenever" command. And it works.
No idea why PATH is misleading the environment.
I hate this problem - I've spent hours trying to solve it too.
What works for me is to add
RAILS_ENV=production; source /usr/local/rvm/scripts/rvm;
before the bundle command.
Forget about PATH settings in cron files. Setting the PATH does not work.
Set the path to bundle explicitly in your config/schedule.rb
set :bundle_command, "/usr/local/bin/bundle"
You can try below solution which I found while googling and that works for me finally....hope that should work with you.
I implemented and tested the same on production make sure that to change environment accordingly -
set :output, "{your path on the server}/log/cron_log.log"
set :environment, :production
env :PATH, ENV['PATH']
job_type :rbenv_rake, %q!eval "$(rbenv init -)"; cd :path && :environment_variable=:environment bundle exec rake :task --silent :output!
Best luck, This issue occurred after 3 years as I was using before just simple what given on the gem documentation on production.
I'm using Ruby 2.x and Rails 4.2 with whenever 0.9.4 latest version. It should work with earlier version as well, if the nature of the issue is same.
thank you.
I think you should try explicitly setting the GEM_HOME and GEM_PATH environment variables in your crontab. You could also try running something like gem list --local or gem environment through cron and checking the output.
I played around with this all afternoon and couldn't find a better solution. Here is what I have come up with
bundle install --binstubs
and then run
bin/rake daily:stats
By executing a command that way: /bin/bash -l -c
You are launching a bash command as a login shell which is going to source (execute) /etc/profile bash file as a setup file. By doing so, if you check this file, it might have bash command lines that erase your previous $PATH which you do not want to since it contains your path to your bundle and all your other commands in the first place.
To fix this issue you just have to remove the lines related to set up the $PATH variable in your /etc/profile file.
This is a ENV['PATH'] not set issue. The most elegant way to fix this is to append the rvm related scripts to the path right after the install. Add the following lines to beginning of .bashrc ( beginning and not end as when .bashrc is accessed by a non-interactive shell, the line [ -z "$PS1" ] && return throws error and the subsequent lines are not executed.
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
and not try to explicitly set PATH and sully environment variables.
For those using rbenv you can use the included shim /home/username/.rbenv/shims/bundle
0 0 * * * /bin/bash -l -c 'cd /home/af/www/app/releases/20120216172204 && RAILS_ENV=production /home/af/.rbenv/shims/bundle exec rake daily:stats --silent >> /home/af/www/app/releases/20120216172204/log/cron.log 2>&1'
in 2021, I found a basic solution, just add on top of schedule.rb
env :PATH, ENV['PATH']
set :output, "log/cron_log.log"
set :runner_command, "rails runner"
from:
https://github.com/javan/whenever/issues/665
I solved this problem by printing out my environmental variables
printenv
finding the ones that look related to Rails. One was a path to gems, the other was GEM_HOME and prepending the command in cron with these two:
PATH=$PATH:/home/petr/gems/bin GEM_HOME=/home/petr/gems program_executable
Also in 2021, adding this in schedule.rb worked for me:
set :job_template, "bash -l -c 'PATH=#{ENV['PATH']} && :job'"
All jobs are by default run with bash -l -c 'command...' (https://github.com/javan/whenever)
So I made bash include ENV['PATH'] in PATH at the beginning and now rails are called from the proper rbenv.
For modern fix, add this line in capistrano deploy.rb,
set :whenever_command, "bundle exec whenever"
[root#smbserver current]# crontab -e
02 22 * * 1-5 /bin/bash -l -c /shell/day.sh
30 14 * * 0 /bin/bash -l -c /shell/week.sh

Resources