Reputation: 509
Is there a reliable way in which a php cli script can detect if another php cli script is running? By running in this case, I mean it would return a row for itself if I did ps aux | grep scriptname.php
in the command line.
This command also tends to return itself in the output however, so I'm worried that if I simply do an exec('ps aux | grep scriptname.php',$output);
that it will return a false positive.
The script I am detecting also makes log entries, but under some conditions it sleeps for up to 5 minutes, so detecting its log entries seems a crude method of detection in this instance.
Upvotes: 0
Views: 654
Reputation: 780673
Here's a slightly simpler version of leftclickben's answer. Wrapping one letter in the scriptname with []
saves you from having to filter out grep
.
exec('ps aux | grep "[s]criptname.php"', $output);
Upvotes: 2
Reputation: 4614
You could use grep -v grep
to filter out the "return itself in the output" part. That is, it will only find scriptname.php
where there is not also grep
in the command:
exec('ps aux | grep scriptname.php | grep -v grep', $output);
Upvotes: 1