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

talexb has asked for the wisdom of the Perl Monks concerning the following question:

I have a Mojolicious monitoring web app that's working well. I'd like to extend it to give it the ability to start scripts running.

My code is something like this:

use Mojolicious::Lite; use Mojo::IOLoop::Subprocess; ... post '/' => sub { ... my $message = 'INFO: Something useful'; my $cmd = 'cd /home/foo/bar && ./some-script.pl >some.out 2>some.e +rr'; my $subprocess = Mojo::IOLoop::Subprocess->new; $subprocess->run( sub { my $subprocess = shift; my $result = qx/$cmd/; return $result; }, sub { my ($subprocess, $err, @results) = @_; if ( $err ) { $message .= "ERROR: $err"; } else { $message = $results[0]; } } ); $subprocess->ioloop->start unless $subprocess->ioloop->is_running; ... $c->render(template => 'index'); };
In the $cmd, I'm cd'ing into a directory and running a known script. The good news is that this works .. the script does actually run fine, and the output and error files are created correctly. What I'm not expecting is that the Mojolicious web app doesn't return immediately while the script runs; rather, it waits for the script to finish.

The behaviour that I'd like is to see that the script started, with the web page refreshing a second or two later. At some point a couple of minutes later, the script will finish .. but I really don't care about the output.

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.