Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preventing timeout on fsockopen

Status
Not open for further replies.

Sparhawk2

Technical User
Jun 8, 2001
7
US
I have written a link checker which loops through a list of links, checks for the recip and tracks trhe results. Each recursion through the loop I set fsockopen timeout to 30 secs, and script time oput to 40 secs.

The problem comes up when the connection has been made, but the GET request stalls. The script will timeout and exit. I have tried regestering a tick function to track the time elapsed and abort the connection if X time has elapsed. Unfortunantly, the tick functioin is apparently not called once the socket connection has been made, so it can't break the connection.

Any ideas how I might limit the time the GET request is allowed to continue?
 
in PHP, there is a wonderful ini function called set_time_limit().

If you place set_time_limit(0); at the beginning of your script, the script will run indefinitely until the process is complete.

Hope this helps.

Chad. ICQ: 54380631
 
I am extending the time limit past the time out for the fsockopen. I need a way to limit the time the GET request is allowed to continue, not let it continue indefinantly.

Since this script will run independantly, I don't want to allow it to run indefinantly. I don't want to take a chance of it running amok and bogging down the site, or server.
 
Well, here is a possibility:

You can use the getmypid() function in script that handles the GET request. You can take this PID, store it in a MySQL or flat file DB along with the timestamp for the start time.

You create a separate script, either a PHP shell script or something that can access this data, and run queries, say every second, and for all processes that extend past the time you want them to run, you grab that PID from the table and kill the Process.

This separate script has to be executed from root.

An example:

Code:
//get script
<?php
$thispid = getmypid();
$thistime = time();

$r = mysql_query(&quot;INSERT INTO my_pids SET(pid,time) VALUES('$thispid','$thistime');

......
?>

Code:
//php shell script set in cron to run every second
#!/usr/local/bin/php -q
<?php
/* maximum allowed runtime in seconds */
$max_runtime = 900; //15 minutes

$dbcon = mysql_pconnect(&quot;localhost&quot;,&quot;user&quot;,&quot;pass&quot;);
@mysql_select_db(&quot;pid_database&quot;,$dbcon);

/* this should work just fine for capturing expired processes */
$r = mysql_query(&quot;SELECT * FROM my_pids WHERE time - NOW() > $max_runtime&quot;);

while($c = mysql_fetch_row($r)) {
     popen(&quot;kill $c[0]&quot;);
}
?>

This should at least get you going and should give you, if anything, a start in the right direction. With a little imagination, I am sure you can use the above as a foundation.

Chad. ICQ: 54380631
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top