Here's what I have and what I need to do:
On the server level:
- it's on EC2
- a PHP Web application server where users upload photo entries.
- there's one flagship Web app server, then there might be other app servers launched by auto-scaling, but all web app belongs to the same load balancer.
- a Mail server, in which I receive photos uploaded from users attached on emails from mobile phones.
- a DB server where photos fetched from emails are inserted temporarily.
On the script side:
- the user can choose from uploading photo either from local PC storage or from her mobile phone. In this case, we are focusing on the latter case.
- a script that is permanently running on the Web application server fetches for new coming photo-emails from the Mail server (say, with a wait interval of 5 seconds)
- that script gets a list of new coming emails and parse to look for photo data, then inserts the binary data to the DB.
- that script is a command executable PHP script.
- if the mail is fetched and photo data inserted successfully, the user proceeds to the metadata input GUI (i.e. title, comment, etc)
Not so complex, but here's the challenge:
- I want to know and make sure that the fetching script is alive, every time a user sends a new photo-email.
Is there any good way to check if a script is alive on a specific server && if it's dead, re-run it && make sure there won't be several processes running the same process?
What I have in mind right now is to use 'ps -ef | grep SCRIPT_NAME' from PHP to know whether the script is alive, but then I'm not sure about how to let only one script running at the same time, when the re-run process is triggered by different users at the same time.
Sorry if my situation/text is confusing, it must be a weird case for most of us, but I'd appreciate any advice on this PHP-Unix coordination...