This is a Mojolicious::Lite answer and has been tested in Windows and OS X:

use Mojolicious::Lite; use File::Temp qw(tempfile); use Proc::Background; use Fcntl qw(SEEK_SET); use IO::Handle; any '/run' => sub { my $self = shift; my ($fh,$filename) = tempfile(); my $script; my $tmp = File::Temp->new( UNLINK => 0, SUFFIX => '.pl'); say($tmp "#!perl\n", "\n", "use IO::Handle;\n", q(open($fh, ">", $ARGV[0]);), "\n", q($fh->autoflush(1);), "\n", q(do { print($fh scalar(localtime), "\n"); sleep 1 } forea +ch (1 .. 20);), "\n", ); $script = $tmp->filename; undef($tmp); $self->app->log->debug("script: $script"); my $command = qq($^X $script $filename); $self->app->log->debug("Command: $command"); my $proc = Proc::Background->new($command); Mojo::IOLoop->stream($self->tx->connection)->timeout(30); $self->render_later; my $recurring = Mojo::IOLoop->recurring(1 => sub { my $tmp_fh = $self->stash->{_fh}; my $told = $self->stash->{_told} // 0; my $proc = $self->stash->{_proc}; $self->app->log->debug("Recurring: " . $proc->pid); my $file = $self->stash->{_filename}; open(my $fh, "<", $file); if (fileno($fh)) { seek($fh, $told, SEEK_SET); read($fh, my $buf, 1024); $self->stash->{_told} = tell($fh); chomp($buf); $self->write_chunk(qq(<script>document.getElementB +yId("stuff").innerHTML += "$buf<br>";</script>\n)); } if ($proc->alive) { return; } $self->app->log->debug("Done"); $self->write_chunk("Done</body></html>"); $self->finish; }); $self->stash->{_recurring} = $recurring; $self->stash->{_fh} = $fh; $self->stash->{_filename} = $filename; $self->stash->{_proc} = $proc; $self->on(finish => sub { my $fh = $self->stash->{_fh}; my $recurring = $self->stash->{_recurring}; Mojo::IOLoop->remove($fh); Mojo::IOLoop->remove($recurring); $self->app->log->debug("Finished"); }); $self->res->code(200); $self->res->headers->content_type('text/html'); $self->write_chunk("<html><body><div id=stuff>Starting...<br>< +/div>"); }; push(@ARGV, 'daemon', '-l', 'http://*:5555') unless @ARGV; app->log->level("debug"); app->secrets(["I Knos# you!!"]); app->start;

In reply to Re: cgi,wait for long running external prog by bpmedley
in thread cgi,wait for long running external prog by noname

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.