how to run bash script in nonblocking mode?

94 views
Skip to first unread message

Caveman Pl

unread,
Apr 21, 2018, 2:46:43 PM4/21/18
to Mojolicious
Hi group,


I just want to run bash script and forget about it.

I was trying just put it in backgrount with '&' sign
`sh runImportPhen &`;
but it  doesn't work.

Because of it I'm trying with Mojo::IOLoop

--------Pdataexport.pm----------------------
package MyApp::Pdataexport;

use base 'Mojolicious::Controller';
use Date::Calc qw(:all);
use File::Path qw(make_path remove_tree);
use Mojo::IOLoop;


sub importExternalPhenotypes{

   ...prepare runImportPhen.sh script...

    my $delay = Mojo::IOLoop->delay;
   $delay->steps(
       sub {
           my $delay = shift;
           `sh runImportPhen `;
       }
       );
    $self->redirect_to('/pdataexport');
}

code above also doesn't work. When I click buton which run  importExternalPhenotypes subroutine I need to wait until bash script ends work - (web page hangs until it finisz)

I was trying also with

sub importExternalPhenotypes{

   ...prepare runImportPhen.sh script...


my $subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess
->run(
  sub {
   
my $subprocess = shift;
           `sh runImportPhen `;
 
}
    $self->redirect_to('/pdataexport');
);



but it crash...

Can you please provide some simple example code how it should be done?

Thank you,

tj



Daniel Mantovani

unread,
Apr 22, 2018, 12:35:34 PM4/22/18
to Mojolicious
Hi tj, I understand that for the run method both subs are required. If you don't want to do anything when the batch finish, the second one could be just sub {} 

i.e.
$subprocess->run(sub { `sh runImportPhen`}, sub {});

BR,

Caveman Pl

unread,
Apr 22, 2018, 1:19:54 PM4/22/18
to Mojolicious
Hi Daniel,

It works!

Thank you,

Tomasz

Caveman Pl

unread,
Apr 24, 2018, 9:49:46 AM4/24/18
to Mojolicious
Hi Daniel,

I found that it works but only once.

When I run batch via web page it is launched an it does its work. And web page is working.
But when I run second batch whens first one is still working web page hangs until second one finisz its work.
Do you know why is that? and how could it be fixed?

Thank you,

tj


W dniu niedziela, 22 kwietnia 2018 18:35:34 UTC+2 użytkownik Daniel Mantovani napisał:

Daniel Mantovani

unread,
Apr 24, 2018, 9:05:59 PM4/24/18
to Mojolicious
Hi tj, I really don't know what could be the issue. Maybe some lock inside the bach program or something like that.

Anyway, you could try this simple mojo app to see that subprocesses should not block among them:

#!/usr/bin/env perl
use Mojolicious::Lite;

get '/:n' => sub {
my $c = shift;
$c->inactivity_timeout(180);
my $n = $c->param('n');
my $sp = Mojo::IOLoop::Subprocess->new;
$sp->run(
sub { `sleep $n` },
sub {
my $pid = shift->pid;
$c->render( text =>
"Subprocess with pid $pid finished after waiting $n segs\n" );
}
);
};

app->start;


Just run it with morbo, and then from one browser window go to http://localhost:3000/30, and from other browser window go for instance to http://localhost:3000/1, etc

You should be able to run many of them simultaneously, and the pids for each subprocess should be different.

BR,
Reply all
Reply to author
Forward
0 new messages