http://www.perlmonks.org?node_id=127546


in reply to Detect Stop Button

You need to check if your script has lost access to STDOUT.

One easy way to make sure your script will stop is to keep it printing info as it processes (and make sure output is not buffered). The script will die as soon as it tries to access STDOUT and it is no longer available.

I tried to check if the script would get an alarm from Apache, but could not get anything (wich is ok, because Apache docs do not talk about signals to CGI scripts when client disconnects ;^)

I'll put my test code here, just in case you want to fidget with it a bit. If all output is placed into $out instead of dumping it straight to STDOUT, the CGI will not stop running until it normaly ends :

#!/usr/bin/perl use strict; print "Content-type: text/plain\n\n"; $| = 1; #dont buffer output to stdout # playing with signals, no luck sub signal { print LOG "got signal, shuting down\n"; close LOG; } $SIG{INT} = \&signal; $SIG{PIPE} = \&signal; $SIG{ALRM} = \&signal; $SIG{HUP} = \&signal; # check until when did the CGI run with log file open(LOG, '>/usr/local/apache/cgi-bin/log.txt') or die("open : $!"); # dont want LOG file buffering my $old_fh = select(LOG); $| = 1; select($old_fh); my $out = "running cgi, placing outout here\n"; for (my $i=0;$i<20;$i++) { # toogle comments in the 2 following lines to see the CGI stop # print "[$i] test cgi<br>\n"; $out .= "[$i]test cgi<br>\n"; print LOG "[$i] test cgi\n"; sleep(1); } print LOG $out; close LOG;

Update : this sample did not work in IIS (as giulienk reported, tks!), but you only need to add a  or die("gone"); after each print. Perl dies in unix CGI land when it cannot access STDOUT, but it lives on in CGI/IIS windows land. And please remember, the snippet *does not* receive signals from apache, the handlers are there just to show that :-)

-- sevensven or nana korobi, ya oki