1

I need to check whether or not an administrative script is already running on Apache, if not then it runs. Currently, I'm loading the server-status contents and checking against it but if I'm checking whether or not the current script is already running it will always return true because I'm running the script to check on itself. Does that make sense?

Basically in "script.php" is something like:

if($this_is_already_running) { exit; }
else { run it }

but it is currently running while it checks on itself so it will always return true.

Any help is greatly appreciated.

1
  • Ah, I thought you were trying to determine whether a script was running on Apache or some other webserver. A better title might be "Checking whether or not a PHP script is currently being served", or something. Commented Jul 9, 2010 at 20:27

2 Answers 2

2

I would use a lock file. Your script would write a file into a pre-defined location, and lock it. Additional instances of your script would check the lock, and if it is in place, exit.

See flock()

the manual has some good examples.

Sign up to request clarification or add additional context in comments.

1 Comment

This would probably work best except I think I'm leaning towards making this a function that will live in an include file to be used in numerous scripts to check against themselves and others. Thanks though, good stuff
1

What about getting the script's PID?

Here's a class for it: http://www.electrictoolbox.com/check-php-script-already-running/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.