I am writing a program that needs to launch a command line tool. I have no trouble launching the program in the background, nor do I have trouble putting it in the background, but the program is launching a database that can take anywhere from a few seconds to an hour to start up depending on the size of the database. There is an indicator that gets spit out to STDOUT that lets me know when the database is actually ready to accept connections. I have been trying variations of fork/exec and IPC::Cmd, which both allow me to see STDOUT and also background the process...but I cannot seem to find a way to wait to move on in my program until after the database is ready to accept connections. Here is the code that currently have:

Can anyone suggest a way to both have the program run in the background and also block until I detect the "ready for connections" message found in STDOUT?

sub cge_start { my($arg_ref) = @_; #=+ iterate over provided config and replace defaults while(my($k,$v) = each %$arg_ref) { #=+ Replace each default value if the key exists. Perhaps paranoi +d, but prevent arbitrary/unexpected settings $arguments{$k} = $v if exists $arguments{$k}; } #=+ Make sure we have a bare minimum of settings return undef if $arguments{dataDir} eq '' || !-d $arguments{dataDir} + || $arguments{imagesPerNode} eq '' || $arguments{nodeCount} eq ''; #=+ Create the results directory # make sure that our directory has a trailing slash "/" $arguments{dataDir} .= '/' unless $arguments{dataDir} =~ m/\/$/; mkdir $arguments{dataDir}.'results', 0744 if !-d $arguments{dataDir} +.'results'; $arguments{resultDir} = $arguments{dataDir}.'results'; #=+ Create the logfile string (does not need to exist) $arguments{logFile} = $arguments{dataDir}.'cge_logfile.log'; #=+ Make sure we have both the number of instances and number of des +ired nodes return (undef,undef) unless $arguments{imagesPerNode} > 0 && $argume +nts{nodeCount} > 0; #=+ Create a cleanup script that we will use to kill "this" server s +ession $arguments{cleanupScript} = $arguments{dataDir}.'cleanup.sh'; #=+ Make sure we have a free TCP port to use my $port = 3750; if(check_port($port)) { $port++; while(check_port($port)) { $port++; } } $arguments{queryPort} = $port; #=+ Concatenate all the non-blank command line arguments my @args; while(my($k,$v) = each %arguments) { push @args, '--'.$k.' '.$v.' ' unless $v eq ''; } my $arg_string = join('',@args); #=+ Run the launcher #exec($cge_launch.' '.$arg_string.'> /dev/null 2>&1 &'); my $ref = run_forked($cge_launch.' '.$arg_string.' &', {stdout_handl +er => sub { my $line = @_; say 'BUFFER: '.Dumper($line); if($line =~ m/Starting port forwarding/) { open(my $CU,'<',$arguments{cleanupScript}) or croak $!; my $text = <$CU>; close($CU); my $pid; if($text =~ m/scancel (\d+)/m) { $pid = $1; } return ($pid,$port); } }}); #my ($success,$error,$buffer_arrayref,$stdout_arrayref,$stderr_array +ref) = run(command => $cge_launch.' '.$arg_string, verbose => 1); # if($success) { # foreach my $line(@$buffer_arrayref) { # say 'BUFFER: '.$line; # if($line =~ m/Starting port forwarding/) { # open(my $CU,'<',$arguments{cleanupScript}) or croak $!; # my $text = <$CU>; # close($CU); # # my $pid; # if($text =~ m/scancel (\d+)/m) { # $pid = $1; # } # return ($pid,$port); # } # } # } #else { return (undef,undef); #} }

In reply to launch long running background program and watch final progress by mabossert

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.