Schedule Cron Jobs in Rails Applications with the "Whenever" Gem

Written by

Ever thought of automating your daily tasks, e.g., sending out birthday emails at night? This is where the cron command comes into play. In this blog post, I explain how cron jobs work and how the “Whenever Gem” can be a good fit for scheduling cron jobs.

What are cron jobs?

A cron job is a scheduled task that runs automatically on a Unix-based operating system. It allows users to schedule commands or scripts to run automatically at a specified time or interval. The scheduled tasks, known as "cron jobs," are defined using a simple syntax in a configuration file called the crontab. Cron is a daemon that runs in the background and checks the crontab file for scheduled tasks to run.

Cron jobs are often used for tasks such as running backups, sending emails, or updating databases. They can be set up to run at specific times, such as every day at 2 A.M., or at specific intervals, such as every hour. The syntax for a cron job is made up of six fields: minute, hour, day of the month, month, day of the week, and the command to be executed.

How to create and manage cron jobs

To create a cron job, a user must first open the crontab file. This can be done by typing crontab -e into the command line. Once the file is open, a new line can be added with the desired schedule and command. For example, to schedule a backup to run every day at 2 A.M., the line 0 2 * * * /path/to/backup.sh would be added to the crontab file.

Cron jobs can also be managed using the commands crontab -l to list all current cron jobs, crontab -r to remove a specific cron job, and crontab -r -u username to remove all cron jobs for a specific user.

It's worth noting that some systems have a specific location for the global crontab, usually located in /etc/crontab, and that's where system-wide cron jobs are placed.

Cron jobs are a powerful tool for automating tasks on a Unix-based system. They can be used for a wide range of tasks, from scheduling backups to sending emails. However, it's important to be careful when creating and editing cron jobs, as a small mistake can have serious consequences.

Syntax for cron jobs:

The syntax for a cron job is made up of six fields: minute, hour, day of the month, month, day of the week, and the command to be executed. Each field is separated by a space, and the fields can contain either a specific value or a wildcard (*). The minute field can be any number between 0-59, the hour field can be any number between 0-23, the day of the month can be any number between 1-31, the month can be any number between 1-12, and the day of the week can be any number between 0-7 (with both 0 and 7 representing Sunday).

For example, to schedule a job to run at 2:30 AM every day, the syntax would be 30 2 * * * command

In this example, the order is (30= minute 2= hour *=any day of the month *= any month *= any day of the week command= the command to be executed)

The above mentioned syntax is hard to understand and relate to real time on the clock. This is a problem that has been solved by “Whenever Gem”.

What is the “Whenever” gem?

The “whenever” gem is a popular Ruby gem that allows developers to easily schedule cron jobs in a Rails application. It provides a simple and intuitive syntax for defining cron jobs, and it automatically generates the necessary crontab entries. The gem allows you to use a clear and easy Ruby syntax to define your cron jobs instead of the traditional and more complex cron syntax.

It also allows you to write your cron jobs in a separate file, making it easier to manage and maintain your cron jobs. The gem also gives you a command-line tool that makes it easy to manage and update your cron jobs.

It's a very useful tool for automating repetitive tasks like sending emails, cleaning up old data, and running backups, and it's easy to integrate into your Rails application.

Why should we use the “Whenever” gem?

There are several reasons why you might want to use the “Whenever” gem in a Rails application:

  1. Simplified Syntax: The gem provides a simple and intuitive syntax for defining cron jobs, which is much easier to read and understand than the traditional cron syntax. This makes it easier to manage and maintain your cron jobs.
  2. Separation of Concerns: The gem allows you to write your cron jobs in a separate file, which keeps your code clean and organized. This separation of concerns makes it easier to understand and manage your codebase.
  3. Easy Integration: The gem is designed to work seamlessly with Rails, and it can be easily integrated into your application. This means you don't have to worry about setting up and managing separate cron daemons or making sure your cron jobs are running correctly.
  4. Command-line tool: The gem provides a command-line tool that allows you to easily update and manage your cron jobs, which makes it easy to make changes to your cron jobs without having to manually edit the crontab file.
  5. Automation: The gem allows you to automate repetitive tasks such as sending emails, cleaning up old data, and running backups, which can save you time and effort.
  6. Flexibility: You can use the gem to schedule tasks for any interval you need, from every minute to every year.

How to integrate the “Whenever” gem:

  1. Add the gem to your application's Gemfile:
gem 'whenever', :require => false
  1. Run bundle install: After adding the gem to your Gemfile, run the following command to install the gem:
bundle install
  1. Generate the initial configuration file: Next, run the following command to generate the initial configuration file for the gem:
wheneverize .

This command will create a config/schedule.rb file in your application's root directory, which is where you'll define your cron jobs.

  1. Define your cron jobs: Next, open the config/schedule.rb file and add your cron jobs using the provided syntax. For example, to schedule a task to run every day at 2 a.m., you would add the following line:
every 1.day, :at => '2:00 am' do
	rake "my_task"
end
  1. Update the crontab: After defining your cron jobs, run the following command to update the crontab with the new jobs:
whenever --update-crontab
  1. Verify that the cron jobs are running correctly: You can use the crontab -l command to check the current crontab and see if your jobs are running as expected.

That's it! Your Rails application is now set up to run the cron jobs you've defined using the “Whenever” gem. You can add, remove, or update the jobs by changing the schedule.rb file, and then updating the crontab with the new configuration by running the whenever --update-crontab command.

In summary

The “Whenever” gem is a useful tool for automating repetitive tasks in a Rails application because it provides a simple syntax, separation of concerns, easy integration, a command-line tool, automation, and flexibility for scheduling tasks.

Frequently Asked Questions