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:
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.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'); };
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.
Back to
Seekers of Perl Wisdom