Question

I would like to include cron tasks in my Capistrano deployment files instead of using the following command to manually edit the crontab file:

crontab -e [username]

Is there a script I could use within the Capistrano run command to set the contents of the crontab?

Was it helpful?

Solution

On my linux box

crontab -u userName -l > fileName

lists the crontab file for userName in fileName.

Then I would use a ruby (or another language) script to update the file.

Finally I would use

crontab -u userName fileName

to update the crontab for userName

OTHER TIPS

Check out the Whenever gem -- this may be stretching beyond what you're intending to do, but it uses very simple (Ruby) syntax and makes it dead simple to setup cron jobs within a capistrano deployment script.

given that you have a variable set that is :new_user

and that you are using use_sudo true

desc "install crontab"
task :install_crontab do
  run "echo '0 23 * * * /home/#{new_user}/scripts/backup_#{new_user}.sh' | #{sudo} crontab -u #{new_user} -"
end
def crontab_add(line)
  config = capture(%Q{crontab -l}).split "\n"
  return if config.include? line
  run %Q{(crontab -l; echo "#{line}") | crontab -}
end

Why not include a crontab that can be installed to /etc/cron.d?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top