2

What would be the best way to add a new cron job to several servers simultaneously, or to automate the creation of a cron job on several servers.

I am planning on using rysnc to push a bash script out to several servers, but I need to add a cron job that will run this script.

5 Answers 5

7

Most modern Linux distributions have support for the /etc/cron.d framework, which would allow a modular approach to pushing cron "snippets" out to multiple servers. This is a special directory that is scanned every minute for available jobs. You can drop small cron files into the directory. It's a more elegant approach than editing a central or per-user crontab.

See: What's the difference between /etc/cron.d and /var/spool/cron? For more information on the slightly-different format needed to use this framework.

I would create the jobs/cron files and scp them to the relevant servers. I think that for something at this scale, Puppet or a full configuration management suite is overkill.

1
  • This is what I ended up doing. Thanks for the support. The files in /etc/cron.d/ are like an extension of the cronttab. One thing to be careful of is that you specify a user to run the job. Nov 6, 2012 at 18:44
6

You can also consider Ansible, it has cron module.

1
  • I do have to remember to add Ansible to my list of Puppet-alternatives.
    – Ladadadada
    Oct 31, 2012 at 14:00
1

Puppet has a cron provider. (probably also CFEngine and Chef and some of the other options)

It also has a neat trick where you can stagger the execution of cron jobs using the hash of the hostname, like this:

cron { "run-myscript":
  command => "/path/to/myscript.sh",
  minute => inline_template("<%= hostname.hash.abs % 60 %>"),
}

If you have more than a couple of dozen servers or you think you are likely to ever grow to that size, getting started now with configuration management will be worth your while.

For a quick-and-dirty solution, clusterssh might do the trick. A longer term solution to these sorts of problems would be MCollective, Func, Fabric or Capistrano.

1
  • 1
    Without puppet, another quick and dirty solution can be to push files to /etc/cron.d/ (present in most linux distro) using scp and a for loop. But using an scm like puppet is recommended.
    – user130370
    Oct 31, 2012 at 13:51
0

I Would write a script that I would put in a samba share that all my server could access, then I will define a the same cron for each and point to the same script!

-1

Should be tried in a proof of concept, but i think it may work with a shared /var/spool/cron directory (such as a nfs share). Did you try it ? I would say it would be the easy way

3
  • This is a very bad idea, nfs lacks security. Anyone on network would be able to push script that edit /etc/shadow.
    – user130370
    Oct 31, 2012 at 16:06
  • I'm not sure to get your point. The NFS directory, will have quite the same permissions than a local /var/spool/cron folder unless you are making a reference to ACLs ?
    – Dodger Web
    Nov 15, 2012 at 10:37
  • nfs was designed in 90's, when security was not a concern. It offer no security at all. You can find many examples on the internet : vulnerabilityassessment.co.uk/nfs.htm
    – user130370
    Nov 15, 2012 at 12:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .