Solved

Never-ending While Loop

Posted on 2010-11-22
3
628 Views
Last Modified: 2013-12-12
So I am writing a mail-import script that will run every minute and check for new email messages from an internal IMAP/POP server... The script will run in multiple instances, one per installation and essentially never be 'done' fetching... at any given time there could be 6-7 installations running at one time (maybe more)

I have part of the script already done, accessing IMAP folders and pulling out vars/header info from mailbox was super easy. Only if a new mID exists (unread) does it proceed with any additional functions; making the majority of the seeks to EXIM fairly light weight. I would really like to stay in PHP (vs Perl) because I have a lot of functions ready-to-go from other post-processes I can call and take advantage of easily.

Here is my question....

a) The below article suggests using sleep() in a never end while() loop and running it as a background process. This was my first thought, essentially I can 'never close' the servers IMAP connection and just keep requesting changes from the mailbox, essentially forcing the connection to stay open in while( ....dothis.. sleep(60) ) and just kick the process once for each installation. I've been reading up on background processing PHP in this situation (never-ending); i've done it with perl/bash but I'm not sure how it would work in PHP; if the PHP timeout limit is reached the script will end, correct? or is this just a function of http PHP processing?

(The suggestion to set PHP limits to '0' unlimited timeout is just not something I'm willing to do, to risky) does that blow this concept for me?

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_22132759.html

b) I could set a cron to go off every minute open/close the IMAP connection each time, set it on a cron (per installation) and kick it every minute but that will run the risk of some processes not ending, before the next begins and essentially run the risk of crashing the server all together, if they piled up.

I'd rather go with theory A but i'm a little unsure and dont want to start one direction only to end in defeat..... So?

Can you while(true){ ..dothis.. sleep() } from shell, without modifying the PHP timeouts. essentially forcing sysadmin to kill the PID to stop the process? Would this have any additional effect server side that would make either or both of these inefficient?
0
Comment
Question by:edjent
3 Comments
 
LVL 1

Author Comment

by:edjent
Comment Utility
Oh Yeah! Thanks for any help that gets me moving down the road!
0
 
LVL 8

Accepted Solution

by:
ropenner earned 500 total points
Comment Utility
for B)  you can avoid crashes if you check within your PHP script first whether the previous one finished ... by looking through the current processes 'ps' or another method of detecting that it finished up previously.  Thus avoiding opening up IMAP folders if still in use.  If the previous one didn't finish then exit the script and wait for the next minute to come around.

A)  I've just tried this and it looks like it works with sleeps without having to touch the timeout.  Writing a log file would be advisable so that you can see what is happening as it sleeps and wakes.

The code I just tried sets the timeout to 2 seconds and then sleeps every second for 10 seconds ... the process is not interrupted from the commandline

<?PHP
set_time_limit(2);

$loop =0;
while(1) {
      $loop++;
      sleep(1);
      print "yo  ";
      if ($loop > 10) {
            exit(1);
      }
}
?>
0
 
LVL 6

Expert Comment

by:rubene
Comment Utility
Maybe you could run your php script as a daemon, because php has wrappers around the common C functions related to daemon processes.

So you could fork child processes from your main php script process and control the child processes from that main process. See: http://php.net/manual/en/function.pcntl-fork.php for info on the pcntl_fork function.Just a suggestion.

If you end up going this way don't forget to use the 'normal' daemon stuff, such as fclosing or redirecting stdin, stdout en stderr to /dev/null.

HTH.
Ruben.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
The viewer will learn how to count occurrences of each item in an array.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now