whenever icon indicating copy to clipboard operation
whenever copied to clipboard

Whenever gem doesn't run on production

Open Rubioli opened this issue 8 years ago • 5 comments

I have whenever installed on my Rails 4.2.4 application and want to run this command: rake punching_bag:combine[14,3,2] for punching_bag gem.

punching_bag gem is a hit tracker & this command combines all posts hits for better performance.

My schedule.rb looks like this:

every 1.day, :at => '3:30 am' do
  rake "punching_bag:combine[2,3,2]"
end

When I run crontab -e in my server Ubuntu 12, I can see the cron job: 30 3 * * * /bin/bash -l -c 'cd /home/productiondeploy/mydomain/releases/201611107983652 && RAILS_ENV=production bundle exec rake punching_bag:combine --silent'

But I can see that the command is not running and posts hits are Not combined. Is it something I do wrong here and how to fix it?

Thanks in advanced!

Rubioli avatar Dec 12 '16 10:12 Rubioli

Do you use RVM or rbenv or similar?

benlangfeld avatar Dec 13 '16 11:12 benlangfeld

This is what I use:

Rails: 4.2.4 Ruby: 2.3.1 RBENV Ubuntu 12

Rubioli avatar Dec 13 '16 11:12 Rubioli

Can you run the task from the command line?

cd /home/productiondeploy/mydomain/releases/201611107983652 && RAILS_ENV=production bundle exec rake punching_bag:combine --silent

Also, are you using the same user for deployments and to run the app?

attilahorvath avatar Mar 06 '17 12:03 attilahorvath

I know it's a couple of months ago and this might be resolved but I see a lot of issues here that people's cronjobs are not running (#655, #665, #628). Mine were not running either. If you don't specify the error output the errors will be sent by mail to the user who created the cronjob. So on your server type mailx. Enter to read unread mail. Here you will probably find your problem.

In my case the error was

Your Ruby version is 1.9.3, but your Gemfile specified 2.3.3

and

Undefined local variable or method 'git_source' for Gemfile

I use Capistrano for deployment with a dedicated user deploy and rbenv for installing Ruby. When the cronjob is run all the environment variables necessary for rbenv are missing. bundle exec rake uses the system Bundler. To solve this I changed the whenever rake jobtype via

# top of schedule.rb
job_type :rake, 'export PATH="$HOME/.rbenv/bin:$PATH"; eval "$(rbenv init -)"; cd :path && :environment_variable=:environment bundle exec rake :task --silent :output'

haffla avatar Jun 22 '17 11:06 haffla

@haffla After chasing my own tail for half a day, I stumbled across your suggestion and it worked like a charm, thanks!

For future generations: the output of

crontab -l

on the server should look something like this:

PATH=/var/www/your-app-here/shared/bundle/ruby/2.6.0/bin:/home/ubuntu/.rbenv/versions/2.6.6/bin:/home/ubuntu/.rbenv/libexec:/home/ubuntu/.rbenv/plugins/ruby-build/bin:/home/ubuntu/.rbenv/plugins/rbenv-vars/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

* * * * * /bin/bash -l -c 'export PATH="$HOME/.rbenv/bin:$PATH"; eval "$(rbenv init -)"; cd /var/www/your-app-here/releases/20210323224359 && RAILS_ENV=production bundle exec rake your_task:here --silent >> log/whenever.log 2>&1'

I couldn't for the life of me figure out why rbenv-vars weren't loading and explicitly adding the export path and rbenv init to the crontab worked!

donrestarone avatar Mar 23 '21 22:03 donrestarone