(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'

Finally, Sync your script to crontab:

whenever --update-crontab


(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.




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.