• how to timeout system command?

    From fidokomik@21:1/5 to All on Fri Aug 3 12:20:37 2018
    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 ...}
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christopher Chavez@21:1/5 to fidokomik on Fri Aug 24 12:09:36 2018
    On 8/3/2018 2:20 PM, fidokomik wrote:
    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 ...}
    }


    Would it be possible for you to instead use a command like `pkexec` as a graphical substitute for `sudo` and avoid needing to timeout?


    ---
    This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From fidokomik@21:1/5 to All on Fri Aug 24 12:21:52 2018
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christopher Chavez@21:1/5 to fidokomik on Mon Sep 10 02:59:34 2018
    On 8/24/2018 2:21 PM, fidokomik wrote:
    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.

    Some more non-Perl suggestions:

    I did come across very hacky ways of timing out a sudo process (though I
    do not understand the details), e.g.
    https://askubuntu.com/a/401536/549800 . All are with the caveat that
    even if sudo does not get stuck waiting for a password, the process to
    be run still gets killed after the timeout.

    There is also the passwd_timeout option in the sudoers file, if you have control over that.


    ---
    This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christopher Chavez@21:1/5 to fidokomik on Mon Sep 10 02:50:37 2018
    On 8/3/2018 2:20 PM, fidokomik wrote:
    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 ...}
    }

    For mounts specifically, I would suggest using a modern and/or
    FUSE-based approach for non-root users, and avoid sudo as a side effect.
    I.e. a command like `gio mount "smb://..."` (used to be `gvfs-mount`
    until recently). It's somewhat more complicated if other user on the
    system (including root) needs access to the mount though.


    ---
    This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)