How to create a cron job(cron tab) for Ruby on Rails Application in a Server wich uses Cpanel

How to create a cron job(cron tab) for Ruby on Rails Application in a Server wich uses Cpanel

In this little guide, I want to share with you about how to configure a cron job in a cpanel server for Rails application, and adittionally if you want you can a use a whenever gem if you want so let’s start with the configuration cron job in cpanel for Rails.

This is my rake task

lib/task/validate_folios.rake


the content
# coding: utf-8
 namespace :admin do
   desc "update status for free folios"
    task :update_folios => :environment do
     User.all.each do | user |
       if user.created_at + 1.month < Date.today and user.folios_gratis!= false
         params = { number_of_valid_folios: 0, free_folios: false}
         user.update_attributes(params)
       end
    end
   end
 end

First you need to go to the cron jobs area in your cpanel:

cron jobs or cron tabs in ruby on rails cpanel

cron jobs or cron tabs in ruby on rails cpanel

And select the configuration time that you want to execute your task

cron jobs configuration time ruby on rails cpanel

cron jobs configuration time ruby on rails cpanel

And then copy the next code in the field called “command” but before change the path depending in your server configurations:


/bin/bash -l -c 'cd /home/myusername/rails_apps/yourapplicationname && RAILS_ENV=production bundle exec rake admin:update_folios --trace --silent >> log/error_updating_folios.log'

If you don’t understand feel free to let me know and maybe if you want and you need I can create a screencast, what do you think about it?

Regards, see you around.

1 Comment
  • Abdul Samad
    Posted at 05:43h, 22 October

    Thanks a lot this (/bin/bash -l -c) works for me after long search …..

Post A Comment