Hi Everyone!. In this post, I want to share with you a little guide that will show you how to use cron jobs in a Ruby on Rails application.
Requirements
- Ruby (I use ruby 2.7.1p83)
- Ruby on Rails (I use Rails 6.0.3.4)
Note: I advise you to install ruby using a version manager likervm orrbenv. Also, I recommend theGoRails Tutorial that will guide you throughout the process.
What's a Cron Job?
A cron job is a process that is scheduled to run at a certain time (every minute, day, week, or month). On Unix systems, we can run and manage these types of processes using thecron tool. The info of the process that will run thecron tool is stored in a file calledcrontab.
Examples of the use of cron jobs are the following:
- Generate reports. e.g, a report of daily payments from an e-commerce application.
- Send reminders to users. e.g, send offer reminders from an ecommerce application.
- Modify the status of the records. e.g, canceling payments that exceed a time limit to pay.
We can see our cron jobs using the following command
crontab-l
To delete user jobs we can use the following command
crontab-r
And to edit our crontab file, we can use the following command
crontab-e
The structure of a cron job is the following
.--------------- minute(0-59) | .------------ hour(0-23)| | .--------- day of month(1-31)| | | .------ month(1-12)| | | | .--- day of week(0-6)(sunday=0 or 7) | | | | |*****commandto execute
An example of how to define a cron job to execute every 5 minutes
*/5**** /home/user/test.rb
Setup the project
Now that we know what's a cron job, we can start to write code. First, we are going to create our rails project.
rails new whenever_example
Add whenever gem
Whenever is a gem that helps us to define cron jobs in a readable way. So, we will add this gem to ourGemfile
.
# Gemfile# Cron jobsgem'whenever',require:false
Install our dependencies
bundleinstall
Create a rake task
We will use a rake task to run in our cron job. First, create a.rake
file.
# Create the rake tasktouchlib/tasks/whenever_test.rake
On our task, we will simply print a message.
# lib/tasks/whenever_test.rakedesc'Whenever rake task test'taskwhenever_call: :environmentdoRails.logger.info"Whenever task"end
To see the rake tasks of our Rails application, we can use the following command.
# See all tasksbundleexecrake--tasks# See our taskbundleexecrake--tasks |grepwhenever_call# rake whenever_call # Whenever rake task test
We can execute our task to check that it works.
bundleexecrake whenever_call
In our logs file we should see the message we define.
taillog/development.log# Whenever task
Define our cron job
First, we have to setup the whenever gem.
bundleexecwheneverize.# [add] writing `./config/schedule.rb'# [done] wheneverized!
This will create theconfig/schedule.rb
where we will define our cron jobs. We will create a simple task that will be executed every 2 minutes.
# config/schedule.rb# ....every2.minutedorake'whenever_call'end
Now, we have to update our crontab file.
whenever--update-crontab
If we list our cron jobs in the crontab file, we will see something like the following.
# crontab file0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58**** /bin/bash-l-c'cd OUR_PROJECT_PATH && RAILS_ENV=production bundle exec rake whenever_call --silent'
If we see in our production logs, we can see the following.
# log/production.logI,[2020-11-15T20:50:02.590009#53971] INFO -- : Whenever taskI,[2020-11-15T20:52:02.914487#54387] INFO -- : Whenever task
As, you can see the logs have 2 minutes of difference.
However, you can see that the task is logging into ourproduction.log
. If you want to run the cron jobs in development environment, we can use the following.
whenever--update-crontab--setenvironment=development
Of course, we could specify more when the task runs. Here are some examples:
Scheduling a task every day at 3:00am
# config/schedule.rb# ....every1.day,at:'3:00 am'dorake'whenever_call'end
# crontab file0 3*** /bin/bash-l-c'cd OUR_PROJECT_PATH && RAILS_ENV=production bundle exec rake whenever_call --silent'
Scheduling a task every 2 days at 3:00am and 5:30pm
# config/schedule.rb# ....every2.day,at:['3:00 am','5:30 pm']dorake'whenever_call'end
# crontab file0 3 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31** /bin/bash-l-c'cd OUR_PROJECT_PATH && RAILS_ENV=production bundle exec rake whenever_call --silent'30 17 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31** /bin/bash-l-c'cd OUR_PROJECT_PATH && RAILS_ENV=production bundle exec rake whenever_call --silent'
Scheduling a task on monday and sunday at 6:00pm
# config/schedule.rb# ....every[:monday,:sunday],at:'6:00 PM'dorake'whenever_call'end
# crontab file0 18** 1,0 /bin/bash-l-c'cd OUR_PROJECT_PATH && RAILS_ENV=development bundle exec rake whenever_call --silent'
If you want to clear the contents of the crontab file, you can also use the following command.
# Clear crontabwhenever-c
Final Words
Thanks for reading this post and you can find more info about whenever in itsofficial github repo.
If you are also interested in know how to create scheduled jobs in Elixir, I recommend you this amazing post3 ways to schedule tasks in Elixir I have learned in 3 years working with it.
Top comments(3)

- LocationWellington, New Zealand
- EducationBachelor of Engineering (Hons), major in Network Engineering
- WorkDeveloper @ Discolabs
- Joined
In a distributed world, whenever is not going to work very well.
You might want to explore further with your background job queue system and ways to schedule jobs with it. Like for Sidekiq, there is Sidekiq Scheduler.
Have fun exploring the work of scheduled tasks/jobs.

- Email
- LocationCaldas, Colombia
- WorkSoftware Engineer at Kommit
- Joined
Hi Daniel!.
For a monolithic Rails application, whenever seems to work fine.
I didn't know about Sidekiq-Scheduler, it looks cool. Definitely, I will give it a look.
Thank you so much for your recommendations!
PS: It would be very helpful if you could create a post talking about distributed systems and scheduled tasks/jobs.

- LocationWellington, New Zealand
- EducationBachelor of Engineering (Hons), major in Network Engineering
- WorkDeveloper @ Discolabs
- Joined
Even with a monolithic application, it will be worth while to package your whole stack into docker containers. Service Oriented Architecture.
App as a single container, database as another container and redis/memcache as another container. You could package this whole stack with a single docker-compose file and your app on a docker image repository. This allows you to move and deploy your full stack with ease, on any machine you are working with.
For further actions, you may consider blocking this person and/orreporting abuse