Timing out commands? 
Author Message
 Timing out commands?

Does anyone have already written any (sub)routines which will execute a
shell command, but monitor the command and time it out if it is taking too
long? I need to execute a "finger" within a script, but when the remote
machine is down, this can hang, causing all sorts of other problems.

--
 Joshua A. Laff, CRL Head Operator, University of Illinois    (217) 333-7408

_______________________________________________________________________________
Disclaimer: If I were speaking for the UofI, I wouldn't be paying tuition.



Mon, 17 Jun 1996 01:22:05 GMT  
 Timing out commands?

:
: Does anyone have already written any (sub)routines which will execute a
: shell command, but monitor the command and time it out if it is taking too
: long? I need to execute a "finger" within a script, but when the remote
: machine is down, this can hang, causing all sorts of other problems.

Check the alarm(n) usage and the catching of SIGALRM, this may help
you.
I've played a bit with them to timeout socket operations and it worked
fine. Not sure if it will work inside a system() call, but I think it
will.
--
Regards,
.--------------------------------------------------.  .-------------.
| Antonio Vasconcelos at The Lisbon $tock Exchange |  | sysadmin &  |
|           "I DO NOT SPEAK FOR BVL !!!"           |  | perl rookie |
|-------------------------------------------------------------------|


`-_________________________________________________________________-'
X-Tagline: If turtles were chickens they would run like the{*filter*}ens.



Mon, 17 Jun 1996 09:40:36 GMT  
 Timing out commands?


Quote:
>Does anyone have already written any (sub)routines which will execute a
>shell command, but monitor the command and time it out if it is taking
>too long?

Here's a command I have, which I call "prod".  It does an "ls" on
something with timeout.  Configure the command, $spin, and $WNOHANG at
will.

--- cut here --- Cut Here --- CUT HERE --- *C*U*T* *H*E*R*E*!!! ---

#! /usr/local/bin/perl

# This command tries to do a /bin/ls on each argument given it.  This is
# done with a timeout so that, if the argument filename is mounted from
# a system that's down, this command (and its caller, presumably an
# interactive shell) doesn't hang waiting for NFS to time out (if ever).


# child process, and the parent waits around for about $spin seconds
# seeing if the child is finished.  If the child has not, the parent
# sends it signal $sig, which should be a lethal signal.

sub usage {
    die("Usage: $0 [ls-options...] files...\n");

Quote:
}




Quote:
}


$spin = 10;
$sig = 'HUP';
$rc = 0;
$| = 1;



Quote:
}

exit($rc);

sub fork {

    # From /usr/include/sys/wait.h.
    local($WNOHANG) = 1;

  FORK:
    {
        print "\n", $_, ":\n";
        if ($pid = fork) {
            for ($i = 0; $i < $spin; ++$i) {
                # For some reason, the latter doesn't work.
                # if (kill('CONT', $pid) == 0)
                if (waitpid($pid, $WNOHANG) != 0) {
                    last FORK;
                }
                sleep 1;
            }
            ++$rc;
            print "(kill $sig";
            if (kill($sig, $pid) == 0) {
                print ' succeeded';
            }
            print ")\n";
        } elsif (defined $pid) {

            die "$0: could not exec $CMD[0]";
        } elsif ($! =~ /No more process/) {
            # EAGAIN, supposedly recoverable for error
            print "$!";
            sleep 5;
            redo FORK;
        } else {
            die "$0: Can't fork: $!\n";
        }
    }

Quote:
}

--
Tim McDaniel, Convex Computer Corporation, Richardson, TX (near Dallas)




Tue, 18 Jun 1996 03:07:14 GMT  
 Timing out commands?


:
: Does anyone have already written any (sub)routines which will execute a
: shell command, but monitor the command and time it out if it is taking too
: long? I need to execute a "finger" within a script, but when the remote
: machine is down, this can hang, causing all sorts of other problems.

Try this one on for size.  It depends on the BSDism limit cputime.
It also sets an death alarm as well.

#!/usr/local/bin/perl
require 'syscall.ph';
require 'sys/resource.ph';

# Start a sub process, and return timing info in an array.
sub spawnntime {



  FORK: {
      if ($child_pid = fork ) {
          waitpid($child_pid,0);        # Parent -  wait for child to finish
          &report_err($cmd) if ( $? );

          $diff[0] = $end[0] - $start[0];
          $diff[1] = $end[1] - $start[1];
          $diff[2] = $end[2] - $start[2];
          $diff[3] = $end[3] - $start[3];
          $child_pid = 0;

      } elsif (defined $child_pid) {
        # Child  -  run argument
          if( $limittime) {

              $timelimit = pack("i2", $limittime+$so_far[0], $limittime*2+$so_far[0]);
              syscall(&SYS_setrlimit, &RLIMIT_CPU, $timelimit);
              alarm($limittime*1.1);
          }
          exec("$cmd");
          &report_err($cmd) if ( $? );
          exit;

      } elsif ( $! =~ /No more process/ ) {
          # EAGAIN, presumably recoverable...
          sleep 5;
          redo FORK;
      } else {
          die "Can not fork: $!\n";
      }
  }

Quote:
}

# Report an error
sub report_err {

    local($err) = $?;
    if($err>>8) {
        printf "$command exited. Code %d \"%s\"\n",
        $err>>8,$msg[$err>>8];
    }
    if($err & 255) {
        printf "$command killed by signal %d \"%s\"\n",
        $err&255, $signal[$err& 255];
    }

Quote:
}

print join(",", &spawnntime("echo Just Another Perl Hacker", 1));

-mac
--


`------' See our AD in EE Times!  For information, call 1-800-VERILOG



Tue, 18 Jun 1996 03:51:20 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Time outs and exit codes

2. Sockets, Time-outs, and Alarms

3. calling several commands at the same time

4. times command on NT

5. Command timed out using Net::Telnet

6. Command timed out using Net::Telnet

7. telnet to Win2k times out after executing command

8. Timing out commands

9. open command and time out???

10. multiple commands at the same time?

11. multiple commands at a time?

12. calling several commands at the same time

 

 
Powered by phpBB® Forum Software