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.

ewwhite
  • 197,159
  • 92
  • 443
  • 809

5 Answers5

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.

ewwhite
  • 197,159
  • 92
  • 443
  • 809
  • 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. – callwithcurrentconfusion Nov 06 '12 at 18:44
6

You can also consider Ansible, it has cron module.

Tom
  • 146
  • 1
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.

Ladadadada
  • 26,337
  • 7
  • 59
  • 90
  • 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. –  Oct 31 '12 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!

user200900
  • 103
  • 3
-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

Frederik
  • 3,359
  • 3
  • 32
  • 46
  • This is a very bad idea, nfs lacks security. Anyone on network would be able to push script that edit /etc/shadow. –  Oct 31 '12 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 '12 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 : http://www.vulnerabilityassessment.co.uk/nfs.htm –  Nov 15 '12 at 12:40