Debian Codegraph @

contrib/pswait

raw
1 #!/usr/bin/perl
2 use strict;
3 use Proc::ProcessTable;
4
5 exit if ( $#ARGV == -1 );
6 $|++;
7 my $ptable = Proc::ProcessTable->new;
8 my %waited = ();
9 my %proc;
10 $proc{ $_->pid }=$_->fname foreach (@{$ptable->table});
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 GetOptions(
16 'e|exit+' => \( my $endfirst = 0 ),
17 'v|verbose+' => \( my $verbose = 0 ),
18 'h|help+' => \( my $help = 0 ),
19 's|sleep=i' => \( my $sleeptime = 1 )
20 );
21 pod2usage(-verbose => 1) && exit if $help;
22
23 foreach (@ARGV) {
24 next if ( $_ == $$ ); # DON'T wait for myself !
25 if (/\d+/) {
26 if ( exists $proc{$_} ) {
27 $waited{$_}++;
28 }
29 else { print "nothing like $_\n" if $verbose }
30 next;
31 }
32 foreach my $p ( keys %proc ) {
33 next if ( $p == $$ ); # DON'T wait for myself I said !
34 $waited{$p}++ if ( $proc{$p} =~ /$_/ );
35 }
36 }
37
38 if ($verbose) {
39 print "I am process $$\n";
40 print "waiting after $_ \n" for ( keys %waited );
41 print "sleep time set to $sleeptime s\n";
42 print "will exit on first terminated process\n" if ( $endfirst );
43 }
44
45 my $count = scalar keys(%waited);
46
47 while ( scalar keys(%waited) ) {
48 sleep $sleeptime;
49 %proc = ();
50 $proc{ $_->pid }++ foreach (@{$ptable->table});
51 foreach my $p ( keys %waited ) {
52 if ( !exists $proc{$p} ) {
53 print "gone $p\n" if $verbose;
54 delete $waited{$p};
55 }
56 }
57 last if (( scalar keys(%waited) < $count ) && ($endfirst));
58 }
59
60 __END__
61
62 =head1 PSWAIT
63
64 pswait - waiting for process(es) to end before doing things
65
66 =head1 SYNOPSIS
67 pswait [options] [process ID|NAME ...]
68
69 =head1 USAGE
70 pswait [options] [process ID|NAME ...]
71
72 Options:
73 -h --help brief help message
74 -v --verbose be verbose
75 -e --exit exit after first process end
76 -s x --sleep x set sleep time to x second (default to 1)
77
78 =head1 OPTIONS
79
80 =over 8
81
82 =item B<-e, --exit>
83
84 When giving a list of process to watch, exit when the first process end, it is usefull to easily manage a pool of proces with a shell script.
85
86 =item B<-h. --help>
87
88 Print a brief help message and exits.
89
90 =item B<-s x, --sleep x>
91
92 set sleep time to x seconds between process checking, default to 1 second. On small machines (or overloaded machines) it could help to check process state only once every minutes instead, using for example -s 60
93
94 =item B<-v, --verbose>
95
96 Inform you on what happening ... which process ends, which process ID are watched for state (usefull when using process names on command line).
97
98 =back
99
100 =head1 DESCRIPTION
101
102 B<pswait> will read the process table of the system
103 and wait for some process to end
104
105 =head1 AUTHOR
106
107 DominiX <[email protected]>
108
109 =cut
110