[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Sheflug] running shell commands asynchronously with perl



>>>>> "Andrew" == Andrew Basterfield <list [at] lostgeneration.freeserve.co.uk> writes:

    Andrew> I've got some cgi perl that runs pppd. The problem is at
    Andrew> the moment the text from pppd appears in the browser all
    Andrew> at once when the command has finished, but I have seen a
    Andrew> website running nmap through cgi which updates as the
    Andrew> command progresses. Is there some magik to this?

    Andrew> I have tried

    Andrew> 	print `pppd call $connection 2>&1`;

    Andrew> and

    Andrew> 	open(shell, "pppd call $connection 2>&1");
    Andrew>  [at] lines=<shell> close(shell);

    Andrew> then printing the contents of the array  [at] lines (which
    Andrew> obviously won't work until the command has returned).


Increment $| to turn off buffering on the currently selected
filehandle (by default STDOUT is selected, which is what you want).

Try the following (untested, and it's late :-) :



#!/usr/bin/perl

$|++;   # Or $| = 1;

open (PPP, "pppd call $connection 2>&1 |") or die "$!";

while (<PPP>) {
   print $_;
}

close (PPP) or die "$!";



Read "perldoc -f open" about piped open and also "perldoc perlipc" and
perldoc IPC::Open2 and IPC::Open3 for more info on IPC.
perldoc perlvar describes $|.

Cheers.

Baz.

-- 
Barrie J. Bremner
baz-sheflug [at] barriebremner.com      http://barriebremner.com/


___________________________________________________________________

Sheffield Linux User's Group -
http://www.sheflug.co.uk/mailfaq.html

  GNU the choice of a complete generation.