Cron to trigger a Rails Rake in DigitalOcean? - ruby-on-rails

I have a rake task that I'd like to schedule a cron to run. My Rails 4 project is hosted on a DigitalOcean droplet is running Ubuntu 12.10 (Nginx + Unicorn).
Here's what I have in crontab:
SHELL=/bin/bash
PATH=/sbin:/bin/usr/bin
HOME=/
MAILTO="xxx#gmail.com"
* * * * * /bin/bash -l -c 'source ~/.bash_profile && cd /home/rails && RAILS_ENV=production bin/rake populate:fetch_nr_songs'
However I get this error:
/etc/profile.d/rvm.sh: line 9: ps: command not found
/etc/profile.d/rvm.sh: line 10: ps: command not found
/usr/local/rvm/scripts/rvm: line 96: dirname: command not found
ERROR: Missing proper 'which' command. Make sure it is installed before using RVM!
Any fix would be much appreciated thanks

Seems like somethings wrong with RVM installation. Try re-installing it https://www.digitalocean.com/community/articles/how-to-install-ruby-on-rails-on-ubuntu-12-04-lts-precise-pangolin-with-rvm
Note: you can effortlessly manage crontab using whenever

Related

Can't execute Rails rake task with cron

I'm trying to run rake task with cron on Ubuntu with rvm
My crontab -l
* * * * * cd /media/sf_visa-tracker/ && /home/ruslan/.rvm/gems/ruby-2.1.0/bin/rake parse RAILS_ENV=production >> /var/log/visa-parse.log 2>&1
which rake says
/home/ruslan/.rvm/gems/ruby-2.1.0/bin/rake
And I keep getting this error in my log (/var/log/visa-parse.log)
/usr/bin/env: ruby_executable_hooks: No such file or directory
Also, if I run cd /media/sf_visa-tracker/ && /home/ruslan/.rvm/gems/ruby-2.1.0/bin/rake parse RAILS_ENV=production from my terminal, rake task works.
Pls help ;)
The simplest way to use rvm with cron is to use rvm's wrappers. Your shell sets up a whole bunch of rvm-related environment when it starts, and that's missing from your cron job. The wrappers are versions of the Ruby-related commands that take care of this for you.
In this case, if you have rvm installed to /usr/local, your cron job should look something like this:
* * * * * cd /media/sf_visa-tracker/ && /usr/local/rvm/wrappers/ruby-2.1.0/bin/rake parse RAILS_ENV=production >> /var/log/visa-parse.log 2>&1
You could also bundle up your rvm setup into a shell script that loads rvm before invoking rake; there's more details of both approaches in rvm's documentation on working with cron.
I struggled to get something that worked and seemed clean. Here's what I ended up doing in case it helps.
crontab
0 5 * * * /home/deploy/apps/myproject/shared/cron/daily_updates.sh
I put the script in myproject/shared since I'm using Capistrano and that seemed like a good place for things that would persist between releases.
shared/cron/daily_updates.sh
#!/usr/bin/env bash
# You will need to update this as you bump ruby versions
source /home/deploy/.rvm/environments/ruby-2.3.1#myproject
# Since I had a separate place for Rails specific environment variables (e.g. secrets, app keys)
# you should set the permissions on that file to be owner read/write only
source /home/deploy/.rails_env
cd /home/deploy/apps/myproject/current
rake RAILS_ENV=production daily_update:send_updates 2>&1

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.

(CRON) error (grandchild #26321 failed with exit status 127)

I am using whenever gem my schedule.rb content is
every 2.minutes do runner
"Refinery::Page.update_publishing_date_to_list_in_menu" end
I get this by crontab -l
# Begin Whenever generated tasks for: /home/harssh/Documents/Aptana Studio 3 Workspacwinbox/wineboxnew/config/schedule.rb
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58
* * * * /bin/bash -l -c 'cd /home/harssh/Documents/Aptana\ Studio\ 3\ Workspacwinbox/wineboxnew && RAILS_ENV=production bundle exec rake page:publish --silent'
# End Whenever generated tasks for: /home/harssh/Documents/Aptana Studio 3 Workspacwinbox/wineboxnew/config/schedule.rb
This runs well on my ubuntu 10.10
But I get (CRON) error (grandchild #26321 failed with exit status 127) on my debian server
where I am using apache and passenger with rvm using Ruby 1.9.3
If I use script in terminal in debian it also runs properly but automated cron task gives error
I am also faced same problem, Finally I resolved this issue.
Problem 1: Ruby path in script/runner file
ruby path in script/runner file:
#!/usr/local/bin/ruby
You can replace (/usr/local/bin/ruby) with your machine ruby path. You can check ruby path with the following command in your machine:
which ruby
Problem 2: I think your runner don't have execution permission.
You can set execution permission to runner file the following way:
Ruby project path/script> chmod +x ruuner

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

Could not find rake on capistrano deploy

So I am attempting to get a capistrano deployment setup with an nginx/unicorn server, using RVM.
The deploy works, but when I attempt to interact with unicorn via capistrano i get an error like:
[~/source/quibbler/config]
$ cap unicorn:start
* executing `unicorn:start'
* executing "cd /u/apps/quibbler/current && bundle exec unicorn_rails -c /u/apps/quibbler/current/config/unicorn-production.rb -E production -D"
servers: ["wwwtestvm.whf.app"]
[wwwtestvm.whf.app] executing command
** [out :: wwwtestvm.whf.app] Could not find rake-0.9.2 in any of the sources
command finished in 553ms
failed: "rvm_path=/usr/local/rvm /usr/local/rvm/bin/rvm-shell 'ruby-1.9.2-p290#quibbler' -c 'cd /u/apps/quibbler/current && bundle exec unicorn_rails -c /u/apps/quibbler/current/config/unicorn-production.rb -E production -D'" on wwwtestvm.whf.app
I have searched the interwebs all night and can not find the solution. If I copy paste the command into my shell it works so I am 99% sure it has to do with the RVM environment not being correct.
I have created a gist of my deploy and unicorn configs:
https://gist.github.com/1375736#file_deploy.rb
https://gist.github.com/1375736#file_unicorn_production.rb
Any help or guidance would be appreciated.
Thanks to sannankhalid answer on this other question i was able to resolve it.
Short answer: add rake to your Gemfile, make sure you run bundle install so your Gemfile.lock gets updated, then deploy to your server, ayay!

Resources