EMB01, thanks for your answer, but my problem hasn't got to do with keeping track of what rows to deal with.
What I need is a way to prevent multiple instances of the program from running.
Main Topics
Browse All TopicsI'm writing a php file that -via crontab- gets executed once every minute to send out mails. I'm not being able to device a method to prevent multiple instances of this program from running simultaneously. I only want ONE instance at a time.
My logic is:
1) Get all running processes using "ps auxwww" (plus some awk magic to get only pids for the specified file)
2) If there is more than one process running, meaning there is me, plus another process, exit gracefully.
The problem is that when I run this (see the code window below), I can ONLY see the process id of the currently running php file and none other. Even without filtering anything out, the output of ps auxw only has one process!
I'm guessing there is some sort of user permission thing going on. How can I go around this?
Thanks!
Context: Dedicated server running Red Hat Enterprise Linux 4 with PHP 5.2.9. I have root shell access.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Sorry, I only know of the alternative I posted above. If you don't mind my asking, what's the problem if the script only runs a few lines before being told to continue or exit.
Here's a shell_exec class I found:
http://us.php.net/manual/e
It's also been attached.
Sorry I can't help much more.
EMB01: "If you don't mind my asking, what's the problem if the script only runs a few lines before being told to continue or exit."
There's no problem at all with that. The real problem is that in my environment, the class you suggest will never work as it relies on exec("ps $PID", $ProcessState) to know whether a process is running or not.
As mentioned in my initial post, executing that command will return ONLY the currently running process and NOTHING else.
tkalchev, I still need to try your option out. Could you explain a little bit what it does? I'm not very proficient in shell scripts and I'm not sure what the -z, the brackets, the && and the "$" do.
ps ax - gives a list of all running processes
grep mailerscript.php - displays only the lines, which contain "mailerscript.php"
grep -v grep - filters the word "grep" out
$(xxx) - executes a command and returns its output, same as `xxx`, but at least for me looks much clear
-z - tests if the following string is empty
&& - executes the following command is the result of the previous one is zero ( http://www.gnu.org/softwar
if [ -z "$(ps ax|grep mailerscript.php|grep -v grep)" ]; then
malerscript.php
fi ,but much more compact :)
So the meaning of the expression is the following :
Give me the list of all running processes, containing "mailerscript.php" and not containing "grep" and if this list is empty, execute mailerscript.php
Business Accounts
Answer for Membership
by: EMB01Posted on 2009-04-20 at 09:07:48ID: 24185934
Does your PHP script check a database?
What I do is add a field to whatever table gets checked by the script for a "processed_on". Then, in the first iteration, simply update the field with 00/00/00. Your script should only check for rows in the table that have a NULL value for the "processed_on" date. Then, when the iteration is complete, update the field again and fill it with the appropriate date like 04/20/09.
Please let me know if you have any questions.