note 50757 added to ref.exec
| From: | svenatnospamgrovestreetdotnet at osu1 dot php dot net | Date: | Wed, 09 Mar 2005 06:42:59 +0000 |
| Subject: | note 50757 added to ref.exec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-86141@lists.php.net to get a copy of this message | ||
Hey,
I had this problem: I wanted to kick off a PHP script that would possibly run for a while. This is
on a web GUI, so there is the problem of a) the user stopping the browser, and b) reloading or
revisiting the page.
Since this will live on a hosted box over which I have little control, am unsure of whether CLI or
the CGI is installed, and didn't want to add Perl to the mix, I came up with this hack.
<?
$logfile = dirname(__FILE__)."/mail.log";
if (file_exists ( $logfile )) {
if ( (time() - filectime( $logfile ))<10 ){
// file exists and was changed less than 10 secs ago
header("Refresh: 2; url=''");
print "process running....<br><br>\n\n";
// the below just inverts the log and prints it
$logcontents = file($logfile);
foreach (array_reverse($logcontents) as $line) {
print $line."<br>\n";
}
} else {
// file exists but has not been changed in 10 secs
print "process finished!<br><br>\n\n";
$logcontents = file($logfile);
foreach ($logcontents as $line) {
print $line."<br>\n";
}
// dump the file so I can run the proces again
unlink ($logfile);
}
} else {
echo "\n\n";
echo "startig process...<br><br>\n\n";
ob_flush();
flush();
$log = fopen($logfile, "a") or die("error opening log\n");
// don't kill this process no matter how long it runs
set_time_limit(0);
// keep running even if the user stops the browser
ignore_user_abort(true);
// dummy stuff. put the real work here
for ($i=0; $i<20; $i++) {
$thisline = "iteration $i";
fputs($log, $thisline."\n");
print $thisline."<br>\n";
ob_flush();
flush();
sleep(1);
}
fclose ($log);
}
?>
This yields:
1st hit:
startig process...
iteration 0
iteration 1
iteration 2
.... adding lines as long as the request is not stopped
on subsequent hits
process running....
iteration 2
iteration 1
iteration 0
and refreshing every 2 secs
and finally:
process finished!
iteration 0
iteration 1
iteration 2
iteration 3
iteration 4
.... rest of log file
Seems to be pretty stable... - any comments in email please.
sven
----
Manual Page -- http://www.php.net/manual/en/ref.exec.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50757
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50757&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50757&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50757&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50757&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50757&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50757&report=yes
Search -- http://master.php.net/manage/user-notes.php