Shell Help

Mark Fowler mark at twoshortplanks.com
Mon May 14 17:27:07 BST 2007


On 14 May 2007, at 16:03, Simon Wistow wrote:

> Anybody got any ideas?

Well, this springs to mind:

   bash -c 'FOO=`uuidgen`; chatty_command" >/tmp/$FOO || cat /tmp/ 
$FOO; rm /tmp/$FOO' &

Or do you want it to run in the foreground for a bit, and only shove  
itself in the background if it's taking a while?

Here be totally untested code:

my $SOMETIME = 10; # seconds to run for
my $buffer = "";

open my $fh, "-|", "chatty_command" or die "Can't run the program";

my $time = time();
while (1) {

   # read the next line
   my $content = readline $fh
   if (!defined($content)) {
     exit if eof $fh;
     # problem!
     print STDERR $buffer;
     exit;
   }
   $buffer .= $content;

   if (time()-$time > $SOMETIME) {
     # we've succeeded.  Run until exit in the background
     use Proc::Daemon;
     Proc::Daemon::Init;
     1 while (<fh>);
     # this should probably do some error checking here?
     exit;
}




More information about the london.pm mailing list