Skip to content

Instantly share code, notes, and snippets.

@jumski
Last active August 29, 2015 14:07
Show Gist options
  • Save jumski/dc7c2de984070430d643 to your computer and use it in GitHub Desktop.
Save jumski/dc7c2de984070430d643 to your computer and use it in GitHub Desktop.
Capistrano crotab tasks for multiple projects deployed from same user

Capistrano + crontab + multiple projects sharing a linux user

  1. put crontab.cap into lib/capistrano/tasks/crontab.cap
  2. create your crontab file (you can use ERB) and put it somewhere
  3. set crontab_template to point to your crontab file, eg:
set :crontab_template, File.expand_path('./crontab.erb', File.dirname(__FILE__))
  1. setup crontab:update when appropriate, eg:
after :publishing, 'crontab:update'
  1. Add role cron to all servers that you want the crontab to be updated.

  2. Enjoy!

namespace :crontab do
task :update do
invoke 'crontab:render'
invoke 'crontab:reload'
end
task :render do
on roles(:cron) do
template_path = fetch :crontab_template
template = ERB.new(File.new(template_path).read).result(binding)
execute :mkdir, "-p #{crontabs_dir}"
upload! StringIO.new(template), crontab_path
end
end
task :reload do
on roles(:cron) do
execute "cat #{crontabs_dir}/* | crontab -"
end
end
def crontab_path
"#{crontabs_dir}/#{fetch :application}"
end
def crontabs_dir
@home_dir ||= capture("echo $HOME").lines.first.chomp
"#{@home_dir}/crontabs"
end
end
# * * * * * command to be executed
# - - - - -
# | | | | |
# | | | | +- - - - day of week (0 - 6) (Sunday=0)
# | | | +- - - - - month (1 - 12)
# | | +- - - - - - day of month (1 - 31)
# | +- - - - - - - hour (0 - 23)
# +- - - - - - - - minute (0 - 59)
# import todays data
5 0 * * * cd <%= fetch :deploy_to %>/current && bin/bundle rake import:today
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment