1

I have a PHP script configured to loop endlessly using set_time_limit(0) and for(;;). The script is coded to listen on a specific directory and wait for files. When files are detected, execute some commands and keep the loop going. So far this has been very successful for my needs, but I have concerns.

Are there any performance disadvantages to creating an infinite loop in PHP? How can I configure my server so that this script never stops running? Or perhaps alerts me when it has stopped running? I'd like it to launch on startup too. I have a SUSE SLES 10 box.

for (;;) {
    // Check for empty directory
    if (isEmptyDir($finishedDir)) {         
        // If so, move on
        continue;
    } else {
       // do stuff.
    }
}
3
  • 1
    No problem with runnin infinite loops. Just remember to sleep once in a while, or else the script will eat CPU cycles up for breakfast. Commented Mar 15, 2013 at 19:31
  • It's possible that you could use a cron. Commented Mar 15, 2013 at 19:50
  • Alright then. I am using sleep() in the 'do stuff' part, but that only occurs when a file is actually detected. Should I be using sleep() at the end of every loop, regardless if a file is found or not? Commented Mar 15, 2013 at 19:59

2 Answers 2

1

This may work, but PHP is not really designed for these kinds of daemon-like jobs. Linux/Unix has tailor-made tools like inotify that are guaranteed to perform better, and that can trigger a PHP script when a change event happens.

See this SO question: Monitor Directory for Changes

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

1 Comment

++ PHP has an Inotify extension.
0

You may consider using memory (eg: memcached, apc) instead of using file because accessing a file is more costly than accessing the memory. And consider using a little sleep()s in order to add time interval.

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.