What is Cron?
Cron is a job scheduling system available in Linux & MacOS operating systems.
It can be used to run any program at any given time.
This includes Ruby code!
If there is a specific recurring task that you would like to run automatically every day, every week or even every hour, then Cron may be what you’re looking for.
Example of tasks include:
- Running a weekly database backup
- Generating a daily report of website activity
- Sending reminder emails
Let’s discover how you can make cron work for you!
Basics Of Cron
Every user can have a cron file where you define the tasks you want to run.
You can edit this file with the
crontab -e command.
Or list scheduled tasks with
Example cron job:
0 0 * * * /opt/rubies/ruby-2.5.1/bin/rake db:backup
The syntax can be a bit tricky & you may not want to edit these files by hand all the time.
Are there any gems to make things easier for us?
How to Use The Whenever Gem
Because cron syntax can be hard to remember we can use a gem like
This gem helps you define cron jobs in Ruby.
Install the gem first.
Then, create the configuration file:
wheneverize . # [add] config/schedule.rb
Open it up.
You’ll see some commented-out examples, delete them if you want.
Let’s say we want a backup job that runs every hour.
Write this inside the “config/schedule.rb” file:
every 1.hour do rake "db:backup" end
- rake (start a rake task defined on your current project, note that
db:backupisn’t defined by default, but you can try
- runner (Ruby class + method, like
- command (system command)
whenever --update-crontab command to generate the cron entries.
You should be able to see your new entry with
This is what I got:
0 * * * * /bin/bash -l -c 'cd /home/jesus/testing && RAILS_ENV=production bundle exec rake db:backup --silent'
Notice that the task will run in
Want to change that?
You can use this setting:
set :environment, "development"
This goes in your
schedule.rb file, at the top.
You can also use this command:
whenever --update-crontab --set environment=development
This creates all the tasks with “development” as the environment when you run this command.
More Whenever Examples
This example shows how you can be more specific.
every :monday, at: "6:00 PM" do runner "Mail.send_weekly_newsletter" end
This example shows how to run the same task multiple times during the day.
Run task twice a day:
every :day, at: ["12:00 AM", "12:00 PM"] do command "rm tmp/testing.txt" end
Whenever Gem Logging & Troubleshooting
Once you have tasks in your crontab file (check with
crontab -l) you’re ready to go.
How do you know if the scheduled tasks are working correctly?
You can set up logging & read the output.
Enable logging by adding this at the top of your
set :output, "log/cron.log"
Remember to run the
whenever --update-crontab command every time you make changes!
Log file still empty?
You’ll have to wait the scheduled time to see something happen.
Btw, if you see this error in the logs:
/bin/bash: bundle: command not found
That means cron isn’t running the same version of Ruby that you’re.
If you’re using a Ruby version manager:
Make sure to load it from the
.bash_profile file, instead of
.bashrc, so your cron tasks can use it.
Alternatives to Whenever
Whenever is a well-maintained gem & it works great.
But what are the alternatives?
Here’s a list:
- Heroku Scheduler, it’s a free job scheduling add-on for Heroku users. Not as flexible as cron itself but it gets things done
- Rufus Scheduler, a Ruby gem that replaces cron with an in-memory scheduler
- Sidecloq & Sidekiq-cron, both are Sidekiq add-ons that add recurring tasks to Sidekiq
Note that both Sidekiq addons want actual cron syntax for scheduling, so it won’t be as nice as whenever syntax.
The good thing is that they integrate with Sidekiq web UI.
You’ve learned how to schedule cron jobs using the whenever gem in Ruby!
Please share this article so more people can benefit from it.
Thanks for reading.