| 1 | #!/usr/local/bin/perl -w | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | timed-process - Run background process for limited amount of time | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | timed-process [-e exit_status] timeout command [<arg> [<arg> ...]] | ||||||||||
| 10 | |||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||
| 12 | |||||||||||
| 13 | This script runs I<command> for a specified amount of time and if it | ||||||||||
| 14 | doesn't finish, it kills the process. If I<command> runs and exits | ||||||||||
| 15 | before the given timeout, B<timed-process> returns the exit value of | ||||||||||
| 16 | I<command>. If I<command> did not exit before I<timeout> seconds, | ||||||||||
| 17 | then B<timed-process> will kill the process and returns an exit value | ||||||||||
| 18 | of 255, unless the -e command line option is set, which instructs | ||||||||||
| 19 | B<timed-process> to return a different exit value. This allows the | ||||||||||
| 20 | user of B<timed-process> to determine if the process ended normally or | ||||||||||
| 21 | was killed. | ||||||||||
| 22 | |||||||||||
| 23 | =cut | ||||||||||
| 24 | |||||||||||
| 25 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 6 | ▶ | use Proc::Background 1.04 qw(timeout_system); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 6 | ▶ | use Getopt::Long; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | 6 | $0 =~ s:.*/::; | |||||||||
| 30 | |||||||||||
| 31 | 0 | ▶ | sub usage { | ||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
| |||||||||||
| 32 | 0 | print <<END; | |||||||||
| 33 | usage: $0 [-e exit_status] timeout command [<arg> [<arg> ...]] | ||||||||||
| 34 | |||||||||||
| 35 | This script runs command for a specified amount of time and if it | ||||||||||
| 36 | doesn't finish, it kills the process. If command runs and exits | ||||||||||
| 37 | before the given timeout, timed-process returns the exit value of | ||||||||||
| 38 | command. If command did not exit before timeout seconds, then | ||||||||||
| 39 | timed-process will kill the process and returns an exit value of 255, | ||||||||||
| 40 | unless the -e command line option is set, which instructs | ||||||||||
| 41 | timed-process to return a different exit value. This allows the user | ||||||||||
| 42 | of timed-process to determine if the process ended normally or was | ||||||||||
| 43 | killed. | ||||||||||
| 44 | END | ||||||||||
| 45 | 0 | exit 1; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 6 | my $exit_status = 255; | |||||||||
| 49 | 6 | Getopt::Long::Configure('require_order'); | |||||||||
| 50 | 6 | ▶ | GetOptions('exit-status=i', => \$exit_status) or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | usage; | ||||||||||
| 52 | 6 | ▶ | if ($exit_status < 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 0 | die "$0: exit status value `$exit_status' cannot be negative.\n"; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 6 | ▶ | @ARGV > 1 or usage; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | |||||||||||
| 58 | 6 | my @result = timeout_system(@ARGV); | |||||||||
| 59 | |||||||||||
| 60 | 3 | ▶ | if ($result[1]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 2 | exit $exit_status; | |||||||||
| 62 | } else { | ||||||||||
| 63 | 1 | exit $result[0] >> 8; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||