views:

321

answers:

5

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?

+2  A: 

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

David Harris
Thanks for posting this solution. I think I will try this.
sbtodd
+3  A: 

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.

bensie
Thanks. That is a neat looking gem.
sbtodd
It's fantastic -- I am using it in production on several app deployments.
bensie
A: 

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

graywh
+1  A: 

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

andres
A: 

You can create a rake task to do the actual crontab mangling. You then call the rake task from capistrano.

Here is an example of such a task that lets you install/update a cron task:

update crontab from rake task

Gaspard Bucher