Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cron job not working in Whenever gem

Tags:

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.

like image 775
Grant David Bachman Avatar asked Sep 25 '11 21:09

Grant David Bachman


People also ask

Can Cronjobs run at the same time?

Cron would start all the jobs pretty much at the same time, and they would execute concurrently. For cleanup jobs like these though, you'd be better off writing a short shell script to run the procedure in a sequential manner.

How do I enable cron jobs in cPanel?

Configuring Cron Jobs in cPanel In cPanel, select Cron Jobs in the Advanced section of the Main Page menu. You will find a table for adding new scripts and setting their time intervals. To simplify configuration, we include a drop-down menu with common settings such as Once Per Week or Once Per Month.

How do you check what Cronjobs are running?

You can use the cat, crontab and other Linux commands to view, list and display all cron jobs. The cron service searches its spool area (usually /var/spool/cron/crontabs) for crontab files (which are named after user accounts); crontabs found are loaded into memory.


1 Answers

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
like image 50
damoiser Avatar answered Sep 27 '22 01:09

damoiser