Changed cvs_man.php -> scm_man.php.
[clearscm.git] / clearadm / processrunning.pl
1 #!/usr/bin/perl
2
3 =pod
4
5 =head1 NAME $RCSfile: processrunning.pl,v $
6
7 Checks to see if a process is running
8
9 =head1 VERSION
10
11 =over
12
13 =item Author
14
15 Andrew DeFaria <Andrew@ClearSCM.com>
16
17 =item Revision
18
19 $Revision: 1.2 $
20
21 =item Created:
22
23 Mon Dec 13 09:13:27 EST 2010
24
25 =item Modified:
26
27 $Date: 2013/05/21 16:42:17 $
28
29 =back
30
31 =head1 SYNOPSIS
32
33  Usage processrunning.pl: [-u|sage] [-ve|rbose] [-deb|ug]
34                           -name <processname>
35
36  Where:
37    -u|sage:   Displays usage
38  
39    -ve|rbose: Be verbose
40    -deb|ug:   Output debug messages
41    
42    -name:     Name of the process to check for.
43
44 =head1 DESCRIPTION
45
46 This script will simply check to see if the process specified is running. Note
47 that it uses ps(1) and relies on the presence of Cygwin when run on Windows
48 systems. 
49
50 =cut
51
52 use strict;
53 use warnings;
54
55 use FindBin;
56 use Getopt::Long;
57
58 use lib "$FindBin::Bin/lib", "$FindBin::Bin/../lib";
59
60 use Display;
61 use OSDep;
62 use Utils;
63
64 my $VERSION  = '$Revision: 1.2 $';
65   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
66   
67 sub restart ($) {\r
68   my ($restart) = @_;
69
70   my ($status, @output) = Execute "$restart 2>&1";
71     
72   unless ($status) {
73     display "Successfully executed restart option: $restart";
74       
75     display $_ foreach (@output);
76   } else {
77     display "Unable to restart process using $restart (Status: $status)";
78       
79     display $_ foreach (@output);
80   } # unless
81   
82   return $status;
83 } # restart
84   
85 # Main
86 error "Cannot use $FindBin::Script when using Windows - hint try using Cgywin", 1 
87   if $ARCH eq 'windows';
88   
89 my ($name, $restart);
90
91 GetOptions (
92   usage       => sub { Usage },
93   verbose     => sub { set_verbose },
94   debug       => sub { set_debug },
95   'name=s'    => \$name,
96   'restart=s' => \$restart,
97 ) or Usage "Invalid parameter";
98
99 Usage 'Extraneous options: ' . join ' ', @ARGV
100   if @ARGV;
101
102 Usage "Must specify process name"
103   unless $name;
104   
105 # Announce ourselves
106 verbose "$FindBin::Script V$VERSION";
107
108 my $opts = $ARCH eq 'cygwin' ? '-eWf' : '-ef';
109
110 my $cmd = "ps $opts | grep -i '$name' | grep -v \"grep -i \'$name\'\"";
111
112 my ($status, @output) = Execute $cmd;
113
114 unless ($status) {
115   display "No process found with the name of $name";
116   
117   $status = restart $restart if $restart;
118   
119   exit $status;
120 } elsif ($status == 2) {
121   error "Unable to execute $cmd (Status: $status) - $!\n"
122       . join ("\n", @output), $status;
123 } # if
124  
125 foreach (@output) {
126   next
127     if /grep -i '$name'/;
128     
129   next
130     if /grep -i $name/;
131   
132   next
133     if /$FindBin::Script/;
134     
135   display "Found processes named $name";
136   exit 0;
137 } # foreach
138
139 display "Did not find any processes named $name";
140
141 exit restart $restart if $restart;
142
143 =pod
144
145 =head1 CONFIGURATION AND ENVIRONMENT
146
147 DEBUG: If set then $debug is set to this level.
148
149 VERBOSE: If set then $verbose is set to this level.
150
151 TRACE: If set then $trace is set to this level.
152
153 =head1 DEPENDENCIES
154
155 =head2 Perl Modules
156
157 L<FindBin>
158
159 L<Getopt::Long|Getopt::Long>
160
161 =head2 ClearSCM Perl Modules
162
163 =begin man 
164
165  Display
166  Utils
167
168 =end man
169
170 =begin html
171
172 <blockquote>
173 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
174 <a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
175 </blockquote>
176
177 =end html
178
179 =head1 BUGS AND LIMITATIONS
180
181 There are no known bugs in this script
182
183 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
184
185 =head1 LICENSE AND COPYRIGHT
186
187 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
188
189 =cut