Mike521 Posted December 13, 2007 Share Posted December 13, 2007 Hi all, I have a web script that calls an external function written in another language. The external function will take a few minutes to run, and then my script grabs the output file it creates. I have no control over the external function, just the ability to trigger it and grab it's output so I needed a way to make my script wait for a little while, and I found the sleep() function. but I think I'm misunderstanding it. the problem is that when I trigger my script via the web, it runs up until the sleep portion, then never seems to wake up. here's my basic code, leaving out the meaningless stuff: // do some meaningless stuff // trigger the external function. it will take at least 2 minutes to run // so lets go to sleep echo "<br>I'm going to sleep for 10 minutes"; //sleep(600); sleep(300); // 5 minutes, we're testing echo "<br>I'm awake"; // grab the output file, play with it for a minute // email a confirmation that we woke up if ( $sendEmailVerification ) { if( mail( $mail_to, $mail_subject, $email_html, $headers) ) { // great echo "<br>email sent"; } else { // something bad happened echo "something bad happened..."; } } everything works perfectly when I don't sleep, except that the output file isn't ready yet. if it doesn't exist, my script crashes. if it does exist from a previous run, then my script uses the old file rather than waiting for the new one. I start my script just by visiting it's URL through firefox, and I think that's where the problem is. I think my browser doesn't wait for the script to finish, and then I guess the web server says "well no one is listening anymore, kill the process" -- or maybe it's the other way around, I dunno. thanks in advance! Quote Link to comment Share on other sites More sharing options...
farkewie Posted December 13, 2007 Share Posted December 13, 2007 Add this to the top of your page ini_set("max_execution_time", "300") it wont "sleep" but will let it just sit the trying for 3 mins, by default php gives you 1 min Quote Link to comment Share on other sites More sharing options...
Mike521 Posted December 13, 2007 Author Share Posted December 13, 2007 damn! it still doesn't wake up. it waits the full amount of time (I even set max execution to 60 seconds longer than sleep time, just to give it some cushion) and then just dies it prints all the echo statements right up to/including "I'm going to sleep" then never wakes up again! my browser gets an hourglass for the max execution time, then after that expires it stops loading, hourglass goes away, but no more output is printed.. I also don't get the confirmation email Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 13, 2007 Share Posted December 13, 2007 How do you trigger the external program? Ken Quote Link to comment Share on other sites More sharing options...
Mike521 Posted December 13, 2007 Author Share Posted December 13, 2007 ok I did some testing, everything works fine up to a 3:59 wait time. at 4:00 it stops waking up. here's my slightly updated code: $sleepTime = 240; // in seconds, this is how long we'll wait for the external function to process ini_set( "max_execution_time", $sleepTime + 60 ); // do some meaningless stuff // trigger the external function. it will take at least 2 minutes to run // so lets go to sleep echo "<br>I'm going to sleep for " . $sleepTime/60 . " minutes"; sleep( $sleepTime ); echo "<br>I'm awake"; // grab the output file, play with it for a minute // email a confirmation that we woke up if ( $sendEmailVerification ) { if( mail( $mail_to, $mail_subject, $email_html, $headers) ) { // great echo "<br>email sent"; } else { // something bad happened echo "something bad happened..."; } } Quote Link to comment Share on other sites More sharing options...
Mike521 Posted December 13, 2007 Author Share Posted December 13, 2007 sorry Ken I was typing that response at the same time you sent yours -- I trigger the external function by calling it in an iframe: /* call the external function */ if ( $callExternalFlag ) { echo "<br><iframe src='$theFunctionURLA$theOptionSetting$theFunctionURLB' width='99%'></iframe>"; } the src turns out to be something along the lines of "http://www.thesite.com/thefunction.mvc?bunchOfArguments=bunchofValues" actually the external function itself only takes about a minute to process, but as the script builds I'll have to call several of these, and I can never really be sure exactly how long it'll take, so I overestimate Quote Link to comment Share on other sites More sharing options...
Mike521 Posted December 13, 2007 Author Share Posted December 13, 2007 I did more searching and added ini_set( "max_execution_time", $sleepTime + 60 ); //this doesn't seem to work set_time_limit( $sleepTime + 60 ); // doesn't work either ignore_user_abort(TRUE); // and neither does this none of them worked, it still stops waking up at 4 minutes or more. I guess I'll contact my host, there must be some sort of setting I don't know about Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.