24

I have an application that contains a bunch of tasks, and every day I want to run a cron job that creates a DayTask for each Task in the database. A Task has_many DayTasks and these daytasks are what users will be checking off every day. I'm using the whenever gem but it doesn't seem to be running at all. Any ideas?

config/schedule.rb

every 1.day, :at => "12:01am" do
  runner "Task.generate_tasks_for_day"  
end

Task.rb

  def generate_tasks_for_day
    Task.all.each do |task|
      task.day_tasks.create(:target_date => Date.today)
    end 
  end 

result of running the 'whenever command'

1 0 * * * /bin/bash -l -c 'cd /home/grant/rails_projects/GoalTwist && script/rails runner -e production '\''Task.generate_tasks_for_day'\'''

Note: I've been changing the times in config/schedule.rb every time I want to test run it.

Grant David Bachman
  • 2,158
  • 3
  • 21
  • 32
  • can I check, when you say "result of running the 'whenever command'" have you also checked this is in `crontab -l`? Also, what OS is this on? I would normally run my server in UTC, and ensure whenever uses your local timezone to convert these to UTC times. – Matthew Rudy Sep 25 '11 at 21:52
  • # Begin Whenever generated tasks for: daytask 42 18 * * * /bin/bash -l -c 'cd /home/grant/rails_projects/GoalTwist && script/rails runner -e production '\''Task.generate_tasks_for_day'\''' # End Whenever generated tasks for: daytask I'm running Linux Mint. And thanks for the heads up about the timezone difference. – Grant David Bachman Sep 25 '11 at 22:43

3 Answers3

76

Finally I have solved how to run the gem Whenever. It's working good on production, but not in development mode (I think that to working good in dev mode you must do some tricks).

Then, these are the processes to do:

  1. install the gem
  2. write your scheduler.rb file
  3. push to the remote server
  4. login to the remote server (for example with ssh)
  5. see if whenever is good uploaded by running in terminal: whenever
  6. update whenever crontab by running: whenever --update-crontab
  7. restart the server crontab (for example in Ubuntu server): sudo service cron restart
  8. check if crontab is good implemented on the server: crontab -l

That's it!

Personally, I prefer to set up my crons directly from the server:

  1. Edit the crontab: crontab -e
  2. Append my cron (e.g. every day at 5:00 AM - can be little different for not-Linux-based server):
    0 5 * * * /bin/bash -l -c 'cd /path_to_my_app/current && RAILS_ENV=production bundle exec rake my_cron_rake'
  3. Check if good implemented: crontab -l
  4. Done
damoiser
  • 6,058
  • 3
  • 40
  • 66
  • 3
    Thanks! I didn't quite understand how it worked at first. I was expecting more magic ;) – Damien Roche Nov 22 '12 at 02:48
  • 3
    I don't get why neither sidekiq or whenever mentions `whenever --update-crontab` – TalkativeTree Mar 23 '14 at 08:13
  • I ran whenever on my prod server and it says 'command not found' although the cron job has been deployed `crontab -l` – Cyzanfar Jan 12 '16 at 01:51
  • 2
    I guess that your problem @Cyzanfar is not strictly related to this question. Anyway I guess that you have forgot to import or wrong installed the ```whenever``` binaries - please check the result of ```which whenever```. Do you run this on another user? Has this user the corrects rights? If the problem still persist, I suggest you to search for similar question or open a new one. – damoiser Jan 13 '16 at 15:37
  • Thanks for the answer @damoiser, if you have some time please take a look at [THIS](http://stackoverflow.com/questions/34756448/cron-job-not-running-on-prod-linux-server-using-whenever-gem) i've posted yesterday. – Cyzanfar Jan 13 '16 at 15:52
  • 1
    this is more of a comment to myself since I wind up here every few months, but hopefully it helps others too: bundle exec whenever --update-crontab on the AWS box – Ryan Crews Apr 15 '16 at 18:01
8

You have to actually register the job with the crontab by running:

whenever --update-crontab

Also if you're trying to get jobs running locally, add :environment => "development" to your task

runner "MyTask.some_action", :environment => "development"

Graham T
  • 968
  • 9
  • 14
0

if whenever command gives you -bash: whenever: command not found, try bundle exec whenever

Dende
  • 545
  • 6
  • 19