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:
Let’s discover how you can make cron work for you!
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?
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
db:backupisn’t defined by default, but you can try
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.
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
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.
Whenever is a well-maintained gem & it works great.
But what are the alternatives?
Here’s a list:
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.