1

I currently have a website that has twice been suspended by my hosting provider for "overusing system resources". In each case, there were 300 - 400 crashed copies of one of my PHP scripts left running on the server.

The scripts themselves pull and image from a web camera at home and copy it to the server. They make use of file locks to ensure only one can write at a time. The scripts are called every 3 seconds by any client viewing the page.

Initially I was confused, as I had understood that a PHP script either completes (returning the result), or crashes (returning the internal server error page). I am, however, informed that "defunct scripts" are a very common occurrence.

Would anyone be able to educate me? I have Googled this to death but I cannot see how a script can end up in a crashed state. Would it not time out when it reaches the max execution time?

My hosting provider is using PHP set up as CGI on a Linux platform. I believe that I have actually identified the problem with my script in that I did not realise that flock was a blocking function (and I am not using the LOCK_NB mask). I am assuming that somehow hundreds of copies of my script end up blocked waiting for a resource to become available and this leads to a crash? Does this sound plausible? I am reluctant to re-enable the site for fear of it being suspended again.

Any insights greatly appreciated.

9
  • I'm not seeing why you would need a file lock for this - or, more to the point, why more than one running script would need to write to the same file. It sounds like you might have an ajax call on a setInterval() when what you should be doing instead is calling setTimeout() in the success handler for the previous ajax call. If you are flock()ing a file, make sure you remember to LOCK_UN it as soon as you are finished with it, and also remember that if you are reading it you don't need LOCK_EX, LOCK_SH will suffice. Commented Aug 15, 2012 at 12:12
  • This sounds like an issue with the provider actually; are they using Apache/suPHP perhaps? Commented Aug 15, 2012 at 12:23
  • @Jack I thought that at first, but this could actually be seen with mod_php (just tested it) - because flock() blocks and when you're running on *nix the script timeout won't occur if the script never gets to the top of the lock queue. It's pretty silly that this is the case, but apparently it is. There is also a note on the set_time_limit() manual page to that effect. Commented Aug 15, 2012 at 12:28
  • @DaveRandom that's true, but eventually one process should eventually stop and release its locks :) Commented Aug 15, 2012 at 12:51
  • @Jack But if you are creating new processes faster than they are finishing (which is conceivable when requesting every 3 seconds a script that then goes off and downloads an image from "a web camera at home", which will likely be sitting on a connection with low upload bandwidth) then the queue will keep getting longer and longer. Of course the simple solution to this would be to not lock the destination file until the source file has already been downloaded to a temporary location. Commented Aug 15, 2012 at 13:09

2 Answers 2

1

Probably the approach I would recommend is to use tempnam() first and write the contents inside (which may take a while). Once done, you do the file locking, etc.

Not sure if this happens when a PUT request is being done; typically PHP will handle file uploads first before handing over the execution to your script.

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

1 Comment

Thanks Jack. Going to re-write my script to give this a try in combination with Dave's suggestion above. Sorry I am a new user and cannot vote up, but appreciated in any event.
0

Script could crash on these two limitations

  • max_execution_time
  • memory_limit

while working with resources, unless you have no other errors in script / check for notice errors too

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.