2 |
## $Id$ |
## $Id$ |
3 |
## --------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.20 2004/06/21 14:13:21 jonen |
6 |
|
## added helper function |
7 |
|
## |
8 |
|
## Revision 1.19 2004/05/11 19:45:30 joko |
9 |
|
## now exporting RUNNING_IN_HELL |
10 |
|
## |
11 |
|
## Revision 1.18 2003/12/05 04:58:04 joko |
12 |
|
## + minor update: doesn't require IPC::Session anymore |
13 |
|
## |
14 |
|
## Revision 1.17 2003/07/02 11:17:32 jonen |
15 |
|
## minor changes |
16 |
|
## |
17 |
|
## Revision 1.16 2003/06/25 22:49:56 joko |
18 |
|
## RUNNING_IN_HELL mode for detach option |
19 |
|
## |
20 |
|
## Revision 1.15 2003/06/24 20:21:12 jonen |
21 |
|
## + changed linux part of run_cmd to use Proc::Background instead of IPC::... |
22 |
|
## |
23 |
|
## Revision 1.14 2003/06/24 20:13:18 joko |
24 |
|
## + sub findpatch |
25 |
|
## + now using findpatch and Proc::Background for win32/perl |
26 |
|
## |
27 |
## Revision 1.13 2003/06/23 20:58:31 joko |
## Revision 1.13 2003/06/23 20:58:31 joko |
28 |
## restructured, hopefully makes Linux and Windows (and *BSD) more compatible... what about IPC::Cmd??? |
## restructured, hopefully makes Linux and Windows (and *BSD) more compatible... what about IPC::Cmd??? |
29 |
## |
## |
83 |
get_chomped |
get_chomped |
84 |
bool2status |
bool2status |
85 |
make_guid |
make_guid |
86 |
|
RUNNING_IN_HELL |
87 |
|
RUNNING_IN_HEAVEN |
88 |
); |
); |
89 |
|
|
90 |
|
|
91 |
use Data::Dumper; |
use Data::Dumper; |
92 |
use POSIX qw( strftime ); |
use POSIX qw( strftime ); |
93 |
#use IPC::Run qw( run timeout ); |
#use IPC::Run qw( run timeout ); |
94 |
use IPC::Run qw( start pump finish timeout run ) ; |
use IPC::Run qw( start pump finish timeout run timer ) ; |
95 |
use Carp; |
use Carp; |
96 |
|
|
97 |
# NEW - 2003-06-23 for Linux (what about *BSD?) |
# NEW - 2003-06-23 for Linux (what about *BSD?) |
98 |
use IPC::Session; |
#use IPC::Session; |
99 |
|
|
100 |
|
use File::Spec; |
101 |
|
use Proc::Background; |
102 |
|
|
103 |
|
|
104 |
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
116 |
} |
} |
117 |
|
|
118 |
sub RUNNING_IN_HELL () { $^O eq 'MSWin32' } |
sub RUNNING_IN_HELL () { $^O eq 'MSWin32' } |
119 |
|
sub RUNNING_IN_HEAVEN () { $^O =~ m/bsd/ } |
120 |
|
|
121 |
|
|
122 |
sub get_executable { |
sub get_executable { |
147 |
|
|
148 |
if (RUNNING_IN_HELL()) { |
if (RUNNING_IN_HELL()) { |
149 |
# Required to adapt to IPC::Run on win32. |
# Required to adapt to IPC::Run on win32. |
150 |
$wrapper = 'cmd.exe /C perl'; |
#$wrapper = 'cmd.exe /C perl'; |
151 |
|
#$wrapper = 'start perl'; |
152 |
|
$wrapper = 'perl'; |
153 |
|
#$wrapper = 'cmd.exe /C'; |
154 |
} else { |
} else { |
155 |
# NEW 2003-06-23 - needed if used with IPC::Session (at Linux) |
# NEW 2003-06-23 - needed if used with IPC::Session (at Linux) |
156 |
# whats about Win32? |
# whats about Win32? |
170 |
my $caption = shift; |
my $caption = shift; |
171 |
my $options = shift; |
my $options = shift; |
172 |
|
|
173 |
#print Dumper($options); |
#print STDOUT "run_cmd - options: ", Dumper($options), "\n"; |
174 |
|
|
175 |
# report - header |
# report - header |
176 |
my $sep = "-" x 60; |
my $sep = "-" x 60; |
177 |
print STDERR $sep, "\n"; |
print STDOUT $sep, "\n"; |
178 |
print STDERR " ", $cmd; |
print STDOUT " ", $cmd; |
179 |
print STDERR " - ", $caption if $caption; |
print STDOUT " - ", $caption if $caption; |
180 |
print STDERR "\n", $sep, "\n"; |
print STDOUT "\n", $sep, "\n"; |
181 |
|
|
182 |
# strip name of executable from full command string |
# strip name of executable from full command string |
183 |
$cmd =~ m/(.+?)\s/; |
$cmd =~ m/^(.+?)\s(.*)$/; |
184 |
my $executable = $1; |
my $executable = $1; |
185 |
|
my $executable_args = $2; |
186 |
|
|
187 |
=pod |
=pod |
188 |
# for unix: check if executable is in local directory, if so - prefix with './' |
# for unix: check if executable is in local directory, if so - prefix with './' |
201 |
if ($basedir) { |
if ($basedir) { |
202 |
-e "$basedir/$executable" or die("$basedir/$executable does not exist."); |
-e "$basedir/$executable" or die("$basedir/$executable does not exist."); |
203 |
$basedir .= '/'; |
$basedir .= '/'; |
204 |
|
$cmd = "$basedir$cmd"; |
205 |
} elsif ($use_path) { |
} elsif ($use_path) { |
206 |
$basedir = ""; |
#$basedir = ""; |
207 |
|
$basedir = findpath($executable); |
208 |
|
#print "basedir: $basedir", "\n"; |
209 |
|
my $abspath = File::Spec->catfile($basedir, $executable); |
210 |
|
#print STDOUT "PATH: ", $abspath, "\n"; |
211 |
|
-e $abspath or die("$abspath does not exist."); |
212 |
|
$cmd = $abspath . ' ' . $executable_args; |
213 |
} else { |
} else { |
214 |
-e $executable or die("$executable does not exist."); |
-e $executable or die("$executable does not exist."); |
215 |
#$basedir = "."; |
#$basedir = "."; |
216 |
#$basedir .= './'; |
#$basedir .= './'; |
217 |
$basedir = ""; |
$basedir = ""; |
218 |
|
$cmd = "$basedir$cmd"; |
219 |
} |
} |
|
$cmd = "$basedir$cmd"; |
|
220 |
|
|
221 |
# V1 - backticks or qq{} |
# V1 - backticks or qq{} |
222 |
#`$cmd`; |
#`$cmd`; |
225 |
# V2 - via 'system' |
# V2 - via 'system' |
226 |
#system($cmd); |
#system($cmd); |
227 |
|
|
228 |
if (not $use_path) { |
#if (not $use_path) { |
229 |
my $application = get_executable($cmd); |
my $application = get_executable($cmd); |
230 |
$cmd = "$application$cmd" if $application; |
$cmd = "$application$cmd" if $application; |
231 |
} |
#} |
232 |
|
|
233 |
|
my @cmd = split(' ', $cmd); |
234 |
|
|
235 |
# V3 - using IPC (optional) |
# V3 - using IPC (optional) |
236 |
if ($options->{async}) { |
if ($options->{async}) { |
237 |
|
|
238 |
#run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; |
#run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; |
239 |
my @cmd = split(' ', $cmd); |
|
240 |
|
print STDOUT "run_cmd[async]: Proc::Background: $cmd", "\n"; |
241 |
|
|
242 |
# V3.1 - using IPC::Run |
# V3.1 - using IPC::Run |
243 |
# |
# |
246 |
if (RUNNING_IN_HELL()) { |
if (RUNNING_IN_HELL()) { |
247 |
|
|
248 |
#my $in; my $out; my $err; |
#my $in; my $out; my $err; |
249 |
print STDERR "run_cmd: IPC::Run: $cmd", "\n"; |
|
250 |
|
#print STDOUT "findpath: ", findpath('rap.pl'), "\n"; |
251 |
|
|
252 |
# no success! |
# no success! |
253 |
#start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
#start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
254 |
# |
# |
255 |
# success on Win32, but seems broken at 'timeout' on linux: |
# success on Win32, but seems broken at 'timeout' on linux: |
256 |
run(\@cmd, timeout(4)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
#run(\@cmd, timeout(3)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
257 |
|
#start(\@cmd, timer(2)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
258 |
|
#start(\@cmd, \undef) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
259 |
|
#start \@cmd or croak("run_cmd: IPC::Run could not start '$cmd'."); |
260 |
|
#start(\@cmd, timeout(1)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
261 |
|
|
262 |
# other tests ;) |
# other tests ;) |
263 |
#$IPC::Run::Timer::timeout = 2000; |
#$IPC::Run::Timer::timeout = 2000; |
264 |
#start $cmd or die("IPC::Run could not start '$cmd'."); |
#start $cmd or die("IPC::Run could not start '$cmd'."); |
265 |
|
|
266 |
|
#my $in; my $out; my $t; |
267 |
|
#my $harness = start( \@cmd, \$in, \$out, $t = timer( 5 ) ) ; |
268 |
|
#my $harness = start( \@cmd ) ; |
269 |
|
#$harness->pump_nb(); |
270 |
|
|
271 |
|
#my $postfix = '2>&1 |'; |
272 |
|
#open PIPE, "$cmd $postfix" or die("run_cmd: could not run in background via open!"); |
273 |
|
|
274 |
|
my $proc1 = Proc::Background->new(@cmd); |
275 |
|
print "pid: ", $proc1->pid(), "\n"; |
276 |
|
|
277 |
|
|
278 |
} else { |
} else { |
279 |
|
|
280 |
print STDERR "run_cmd: IPC::Session: $cmd", "\n"; |
#print STDOUT "run_cmd: IPC::Session: $cmd", "\n"; |
281 |
|
|
282 |
# V3.2 - using IPC::Session |
# V3.2 - using IPC::Session |
283 |
# success on Linux AND Win32 ?? |
# success on Linux AND Win32 ?? |
284 |
# |
# |
285 |
# set timeout: |
# set timeout: |
286 |
# (don't really know why we needs 2 secconds |
# (don't really know why we needs some secconds |
287 |
# to wait for init of process !?!) |
# to wait for init of process !?!) |
288 |
my $session_timeout = 3; |
#my $session_timeout = 15; |
289 |
# set session name (default: cmd as string): |
# set session name (default: cmd as string): |
290 |
my $session_name = $cmd; |
#my $session_command = $cmd; |
291 |
# create session: |
#my $session_shell = "/bin/sh"; |
292 |
my $session = IPC::Session->new($session_name, $session_timeout); |
# create session (beware of using '->new' here!?): |
293 |
|
#my $session = new IPC::Session($session_shell, $session_timeout); |
294 |
|
|
295 |
# send 'cmd' to session - not required since complete command is sent via constructor above |
# send 'cmd' to session - not required since complete command is sent via constructor above |
296 |
$session->send(\@cmd); |
#$session->send($cmd); |
297 |
|
|
298 |
#print $session->stdout(), "\n"; |
#my $output = $session->stdout(); |
299 |
|
#print "WS::Admin started.\n" if $output = ' '; |
300 |
|
|
301 |
|
# tests |
302 |
|
#$session->send("echo hello"); |
303 |
|
#chomp(my $hello = $session->stdout()); |
304 |
|
#print "ok 3\n" if $hello eq "hello"; |
305 |
|
|
306 |
# optional switch case: |
# optional switch case: |
307 |
#for ($session->stdout()) { |
#for ($session->stdout()) { |
308 |
|
# /_bootDataBases/ && do { print "WS::Admin started.\n" }; |
309 |
#} |
#} |
310 |
# optional get error: |
# optional get error: |
311 |
#my $err = session->stderr(); |
#my $err = session->stderr(); |
312 |
|
#print "ERR: " . Dumper($err) . "\n"; |
313 |
|
|
314 |
|
#open( *OUT, ">out.txt" ) ; |
315 |
|
#open( *ERR, ">err.txt" ) ; |
316 |
|
#run(\@cmd, \undef, \*OUT, \*ERR ) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
317 |
|
#my ($out, $err); |
318 |
|
#my $h = start(\@cmd, \undef, \*OUT, \*ERR ) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
319 |
|
#finish $h if $err =~ /error/; |
320 |
|
|
321 |
|
# get child pid |
322 |
|
#my $kid; |
323 |
|
#do { $kid=wait(); } until $kid > 0; |
324 |
|
#print "Child PID: " . $kid . "n"; |
325 |
|
|
326 |
|
#finish $h; |
327 |
|
|
328 |
|
|
329 |
|
# test using Proc::Background - success !! |
330 |
|
my $proc = Proc::Background->new($cmd); |
331 |
|
my $kid = $proc->pid(); |
332 |
|
print STDOUT "run_cmd: Proc::Background: $cmd, child PID $kid", "\n"; |
333 |
|
$proc->wait(); |
334 |
|
$proc->die(); |
335 |
|
|
336 |
} |
} |
337 |
|
|
338 |
|
|
339 |
|
} elsif ($options->{detach}) { |
340 |
|
|
341 |
|
if (RUNNING_IN_HELL()) { |
342 |
|
print STDOUT "run_cmd[detach]: Proc::Background: $cmd", "\n"; |
343 |
|
my $proc1 = Proc::Background->new(@cmd); |
344 |
|
print "pid: ", $proc1->pid(), "\n"; |
345 |
|
} else { |
346 |
|
print STDOUT "run_cmd[detach]: system('$cmd' &).", "\n"; |
347 |
|
system($cmd . ' &'); |
348 |
|
} |
349 |
|
|
350 |
} else { |
} else { |
351 |
print STDERR "run_cmd: system('$cmd').", "\n"; |
print STDOUT "run_cmd: system('$cmd').", "\n"; |
352 |
|
#print $ENV{PERL5LIB}, "\n"; |
353 |
system($cmd); |
system($cmd); |
354 |
} |
} |
355 |
|
|
356 |
print STDERR "run_cmd: ready.", "\n"; |
print STDOUT "run_cmd: ready.", "\n"; |
357 |
|
|
358 |
} |
} |
359 |
|
|
406 |
return $guid; |
return $guid; |
407 |
} |
} |
408 |
|
|
409 |
|
# [modified] from: http://www.mit.edu/afs/athena/contrib/watchmaker/src/pt/Configure |
410 |
|
sub findpath { |
411 |
|
#local($path); |
412 |
|
#local($arg) = shift; |
413 |
|
my $arg = shift; |
414 |
|
my $path; |
415 |
|
#for my $dir (split(/:/,$ENV{'PATH'})) { |
416 |
|
for my $dir (File::Spec->path()) { |
417 |
|
#if (-x "$dir/$arg" && -f _) { |
418 |
|
#print STDOUT "scanning: ", "$dir/$arg", "\n"; |
419 |
|
if (-e "$dir/$arg") { |
420 |
|
#$path = "$dir/$arg"; |
421 |
|
#$path = "$dir/"; |
422 |
|
$path = $dir; |
423 |
|
last; |
424 |
|
} |
425 |
|
} |
426 |
|
$path; |
427 |
|
} |
428 |
|
|
429 |
1; |
1; |
430 |
__END__ |
__END__ |