Jonathan Bennett

Kamal + Cron? There's a Better Way

Migrating cron tasks to a Dockerized environment can be tricky—especially since running cron as a non-root user inside a container isn’t straightforward.

Fortunately, there’s a great alternative: Clock. It’s perfect for handling cron-like functionality, especially if you mostly work with rake tasks and job scheduling.

Here’s how to set it up in just three steps:

1. Add Clock to Your Gemfile

Add the gem to your project dependencies:

# Gemfile
gem "ruby-clock", require: false

2. Use the Clock DSL to Define Tasks

Port your existing cron tasks into Clock’s DSL. These definitions should go into a file named Clockfile in the root of your project. Here’s an example:

using RubyClock::DSL

cron '0 * * * *' do
  rake 'my_rake_task'
end

3. Update Your Kamal Deploy Config

Add the configuration to your deploy.yml file to specify where Clock runs:

servers:
  web:
	- 1.2.3.4
  cron:
	hosts:
	  - 2.3.4.5
	cmd:
	  bundle exec clock

Clock’s documentation offers plenty of advanced options, but the above is enough to get started. It’s straightforward, flexible, and integrates perfectly with modern Rails apps.

Have you tried Clock or migrated cron tasks with Kamal? Hit reply and let me know how it went!