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 ...}
}
The user should not see that for many reasons :) User do not speak English, but Raspberry yes, user do not know passwords, user know nothing about LAN.
Generally, I want to run a system command with a defined timeout, and if not success, then run some Perl action or ask the user for basic questions in their language.
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 ...}
}
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 379 |
Nodes: | 16 (2 / 14) |
Uptime: | 84:39:47 |
Calls: | 8,091 |
Files: | 13,069 |
Messages: | 5,851,128 |