Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added with-timeout

  • Loading branch information...
commit 6f34b1856b013ca4f5f3d1ddfde008b23f147dad 1 parent 13e058a
@rurban authored
Showing with 48 additions and 0 deletions.
  1. +48 −0 with-timeout
View
48 with-timeout
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+# with-timeout --- run process but kill if it hangs
+# Author: Noah Friedman <friedman@splode.com>
+# Created: 1996-05-26
+# Public domain.
+
+# $Id: with-timeout,v 1.4 2000/01/17 04:11:13 friedman Exp $
+
+# exec ${PERL-perl} -Sx $0 ${1+"$@"}
+# !perl
+
+sub spawn
+{
+ my $pid = fork;
+ die if (!defined $pid);
+ exec (@_) if ($pid == 0);
+ return $pid;
+}
+
+sub sigalrm_handler
+{
+ printf (STDERR "with-timeout: @ARGV: Timeout waiting for process to complete.\n");
+ kill (1, $proc);
+ exit (1);
+}
+
+sub main
+{
+ if ($#ARGV < 1)
+ {
+ my $p = $0;
+ $p =~ s|.*/||;
+ print STDERR "Usage: $p [timeout] [command {args ...}]\n";
+ exit (1);
+ }
+
+ $SIG{'ALRM'} = \&sigalrm_handler;
+ alarm ($ARGV[0]);
+ shift @ARGV;
+
+ $proc = spawn (@ARGV);
+ wait;
+ exit ($?);
+}
+
+main();
+
+# with-timeout ends here
Please sign in to comment.
Something went wrong with that request. Please try again.