(A) Solution for whenever gem

When configuring rake task to run in crontab via Whenever, rake task is translated to:

... bundle exec rake your:task ...

However, in some environment, the Cron job runs into an error:

bundle: command not found

This can be solved by adding path environment variable into your schedule.rb:

env :PATH, ENV['PATH'] #Add this line
set :output, {:error => '/path/to/cron_error.log', :standard => '/path/to/cron.log'}

every '0 0 1 * *' do
  rake 'your:task'
end

Finally, Sync your script to crontab:

whenever --update-crontab


Updated:

(B) Solution for Cron Job

Alternatively, if you want to add the environment path without going through Whenever. You can follow these steps.

(1) Go to your application directory.

cd /path/to/app

(2) Run rails console.

rails c

(3) Execute the following command in rails console. This will print the entire path string.

ENV['PATH']

Example:

"/home/ubuntu/.rbenv/versions/2.2.5/bin:/home/ubuntu/.rbenv/libexec:/home/ubuntu/.rbenv/plugins/ruby-build/bin:/home/ubuntu/.rbenv/plugins/ruby-build/bin:/home/ubuntu/.rbenv/shims:/home/ubuntu/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

Copy the path string and exit the console (Ctrl+C).

(4) Edit crontab.

crontab -e

(5) Set the path on the first line in crontab file and save. E.g.

PATH=/home/ubuntu/.rbenv/versions/2.2.5/bin:/home/ubuntu/.rbenv/libexec:/home/ubuntu/.rbenv/plugins/ruby-build/bin:/home/ubuntu/.rbenv/plugins/ruby-build/bin:/home/ubuntu/.rbenv/shims:/home/ubuntu/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games