1

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.

thankyou

4

2 回答 2

2

对我来说,我会这样做:

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.
于 2013-04-22T06:57:33.707 回答
1

每台机器都有不同的主机名,因此您可以使用它仅在所需机器上执行作业。例如,如果机器是“server01”、“server02”等,那么在 PHP 中你可以这样做:

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

另一种方法是,不是为所有服务器提供一个服务器映像,而是使用两个略有不同的映像,一个用于“主”服务器(具有 cron 任务等),另一个用于所有其他服务器(“辅助”)服务器。

于 2013-04-22T06:49:59.767 回答