fidokomik
2018-08-03 19:20:37 UTC
I need to run some system command on Linux OS, say "sudo mount ..." from Perl Tk script. But in some situations this command wait for user interaction and I want to terminate it with defined timeout. I tried to use alarm(), IPC::Run, IPC::System::Simple but nothing work correctly. I have no other idea how to do it. I must to say too: it will run on Raspbian (Debian like) distribution.
Snippet of my code:
use strict;
use Tk;
...
our $svr='//192.168.1.1/pi';
our $mnt='/mnt/pi';
our $lgn='pi';
our $pwd='pi';
...
$mw->after(50, \&init);
MainLoop;
sub init {
my $x;
$SIG{ALRM} = sub {$x="error TIMEOUT"; die "error mount\n";};
alarm 5;
# next command need to ignore if not success for 5 second
$x=qx(sudo -t cifc mount $svr $mnt -o username=$lgn,password=$pwd,noperm 2>&1);
alarm 0;
if ($x=~/error/) {print "mount fail\n"}
else { # do some ...}
}
Snippet of my code:
use strict;
use Tk;
...
our $svr='//192.168.1.1/pi';
our $mnt='/mnt/pi';
our $lgn='pi';
our $pwd='pi';
...
$mw->after(50, \&init);
MainLoop;
sub init {
my $x;
$SIG{ALRM} = sub {$x="error TIMEOUT"; die "error mount\n";};
alarm 5;
# next command need to ignore if not success for 5 second
$x=qx(sudo -t cifc mount $svr $mnt -o username=$lgn,password=$pwd,noperm 2>&1);
alarm 0;
if ($x=~/error/) {print "mount fail\n"}
else { # do some ...}
}