Take the 2-minute tour ×

I'm using AWS Amazon Web Services and I have 4 identical Web Servers. I have a cron job setup to sent out emails that only needs to run from one server but exists on all 4.

Everything on these web servers is identical which suits me but currently the cron job runs on all 4. I therefore looking for a solution on how to have the job on all 4 servers but only run on one.

I do have a database and thought of using a tmp table to the first to write/register itself in that table would then be the only one to run (the others would read this and then not execute past this point). But I was concerned because they all use an identical system clock and therefore might read the table at the same time and even though one writes to it the others assume they also have access to run.

Would using a table be able to restrict 4 webservers to only 1 contined or would the identical clock factor get in the way?

Note: the server names have the potential of changing with time as they are more like resources then permanent systems. (eg: can spin new ones up when required)...

Is there a better way to do this? Note: I don't want a different server and I want them all identical.


share|improve this question

2 Answers 2

up vote 2 down vote accepted

For me I would do:

1 - create a lock for each of the server
2 - define a list of server like ['1'=>'server ip', '2'=>'server ip',...]
3 - pass in server id in cron job as args
4 - modify the script if (lock not exists) do run then create a lock for this server, delete the lock for next server.
5 - delete the lock for the first server to start with.
share|improve this answer

The machines will each have different hostnames, so you could use that to only execute the job on the desired machine. For example if the machines are "server01", "server02", etc, then in PHP you might do:

if (gethostname() === 'server01') {
    // do the task

Another way is, instead of having a single server image for all servers, have two slightly different images, one is for the "primary" server (which has the cron tasks, etc) and the other is for all other ("secondary") servers.

share|improve this answer
server names aren't permanent but fair call. 2 images is possible - would rather a way to keep them identical but once again a fair call :) thankyou –  Adam Apr 22 '13 at 6:56

Your Answer


