Removed /usr/local from CDPATH
[clearscm.git] / clearadm / processrunning.pl
1 #!/usr/bin/env 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 $_ for (@output);
76   } else {
77     display "Unable to restart process using $restart (Status: $status)";
78       
79     display $_ for (@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 for (@output) {
126   next if /grep -i '$name'/;
127   next if /grep -i $name/;
128   next if /$FindBin::Script/;
129     
130   display "Found processes named $name";
131
132   exit 0;
133 } # for
134
135 display "Did not find any processes named $name";
136
137 exit restart $restart if $restart;
138
139 =pod
140
141 =head1 CONFIGURATION AND ENVIRONMENT
142
143 DEBUG: If set then $debug is set to this level.
144
145 VERBOSE: If set then $verbose is set to this level.
146
147 TRACE: If set then $trace is set to this level.
148
149 =head1 DEPENDENCIES
150
151 =head2 Perl Modules
152
153 L<FindBin>
154
155 L<Getopt::Long|Getopt::Long>
156
157 =head2 ClearSCM Perl Modules
158
159 =begin man 
160
161  Display
162  Utils
163
164 =end man
165
166 =begin html
167
168 <blockquote>
169 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
170 <a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
171 </blockquote>
172
173 =end html
174
175 =head1 BUGS AND LIMITATIONS
176
177 There are no known bugs in this script
178
179 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
180
181 =head1 LICENSE AND COPYRIGHT
182
183 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
184
185 =cut