Skip to content

Exclusive cron task lock with flock

John Bachir edited this page Feb 1, 2020 · 3 revisions

If you need ensure to run cron task only once at time, you can use file lock with flock

Add those templates to your schedule.rb

# usage script_with_lock 'script_name', lock: 'lock_name'
job_type :script_with_lock, "cd :path && :environment_variable=:environment flock -n /var/lock/:lock.lock :bundle_command script/:task :output"
# usage runner_with_lock 'ruby code', lock: 'lock_name'
job_type :runner_with_lock, "cd :path && flock -n /var/lock/:lock.lock :bundle_command :runner_command -e :environment ':task' :output"