2 |
## $Id$ |
## $Id$ |
3 |
## --------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.17 2003/07/02 11:17:32 jonen |
6 |
|
## minor changes |
7 |
|
## |
8 |
|
## Revision 1.16 2003/06/25 22:49:56 joko |
9 |
|
## RUNNING_IN_HELL mode for detach option |
10 |
|
## |
11 |
|
## Revision 1.15 2003/06/24 20:21:12 jonen |
12 |
|
## + changed linux part of run_cmd to use Proc::Background instead of IPC::... |
13 |
|
## |
14 |
|
## Revision 1.14 2003/06/24 20:13:18 joko |
15 |
|
## + sub findpatch |
16 |
|
## + now using findpatch and Proc::Background for win32/perl |
17 |
|
## |
18 |
|
## Revision 1.13 2003/06/23 20:58:31 joko |
19 |
|
## restructured, hopefully makes Linux and Windows (and *BSD) more compatible... what about IPC::Cmd??? |
20 |
|
## |
21 |
## Revision 1.12 2003/06/23 19:43:19 joko |
## Revision 1.12 2003/06/23 19:43:19 joko |
22 |
## minor cleanup |
## minor cleanup |
23 |
## now using IPC::Session::NoShell |
## now using IPC::Session::NoShell |
80 |
use Data::Dumper; |
use Data::Dumper; |
81 |
use POSIX qw( strftime ); |
use POSIX qw( strftime ); |
82 |
#use IPC::Run qw( run timeout ); |
#use IPC::Run qw( run timeout ); |
83 |
use IPC::Run qw( start pump finish timeout run ) ; |
use IPC::Run qw( start pump finish timeout run timer ) ; |
84 |
use Carp; |
use Carp; |
85 |
|
|
86 |
# NEW - 2003-06-23 |
# NEW - 2003-06-23 for Linux (what about *BSD?) |
87 |
use IPC::Session::NoShell; |
use IPC::Session; |
88 |
|
|
89 |
|
use File::Spec; |
90 |
|
use Proc::Background; |
91 |
|
|
92 |
|
|
93 |
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
114 |
# => better use absolute path-names only?! |
# => better use absolute path-names only?! |
115 |
my $application = ''; |
my $application = ''; |
116 |
if ($cmd =~ m/\w+\.pl\s*.*/) { |
if ($cmd =~ m/\w+\.pl\s*.*/) { |
117 |
# NEW 2003-06-23 - needed if used with IPC::Session (at Linux) |
$application = get_interpreter_wrapper($cmd, 'perl'); |
118 |
# whats about Win32? |
#$cmd = "$application $cmd" if $application; |
119 |
$application = 'perl '; |
$application .= ' '; |
120 |
|
|
121 |
} else { |
} else { |
122 |
$application = './'; |
$application = './'; |
123 |
} |
} |
124 |
return $application; |
return $application; |
125 |
} |
} |
126 |
|
|
127 |
sub get_executable_wrapper { |
sub get_interpreter_wrapper { |
128 |
my $cmd = shift; |
my $cmd = shift; |
129 |
my $application = ''; |
my $language = shift; |
130 |
# Required to adapt to IPC::Run on win32. |
$language ||= ''; |
131 |
#if (RUNNING_IN_HELL()) { |
|
132 |
#$application = 'cmd.exe /C'; |
my $wrapper = ''; |
133 |
#} |
|
134 |
return $application; |
if ($language eq 'perl') { |
135 |
|
|
136 |
|
if (RUNNING_IN_HELL()) { |
137 |
|
# Required to adapt to IPC::Run on win32. |
138 |
|
#$wrapper = 'cmd.exe /C perl'; |
139 |
|
#$wrapper = 'start perl'; |
140 |
|
$wrapper = 'perl'; |
141 |
|
#$wrapper = 'cmd.exe /C'; |
142 |
|
} else { |
143 |
|
# NEW 2003-06-23 - needed if used with IPC::Session (at Linux) |
144 |
|
# whats about Win32? |
145 |
|
$wrapper = 'perl'; |
146 |
|
} |
147 |
|
|
148 |
|
} else { |
149 |
|
die("No wrapper for language '$language'."); |
150 |
|
} |
151 |
|
|
152 |
|
return $wrapper; |
153 |
} |
} |
154 |
|
|
155 |
|
|
158 |
my $caption = shift; |
my $caption = shift; |
159 |
my $options = shift; |
my $options = shift; |
160 |
|
|
161 |
#print Dumper($options); |
#print STDOUT "run_cmd - options: ", Dumper($options), "\n"; |
162 |
|
|
163 |
# report - header |
# report - header |
164 |
my $sep = "-" x 60; |
my $sep = "-" x 60; |
165 |
print STDERR $sep, "\n"; |
print STDOUT $sep, "\n"; |
166 |
print STDERR " ", $cmd; |
print STDOUT " ", $cmd; |
167 |
print STDERR " - ", $caption if $caption; |
print STDOUT " - ", $caption if $caption; |
168 |
print STDERR "\n", $sep, "\n"; |
print STDOUT "\n", $sep, "\n"; |
169 |
|
|
170 |
# strip name of executable from full command string |
# strip name of executable from full command string |
171 |
$cmd =~ m/(.+?)\s/; |
$cmd =~ m/^(.+?)\s(.*)$/; |
172 |
my $executable = $1; |
my $executable = $1; |
173 |
|
my $executable_args = $2; |
174 |
|
|
175 |
=pod |
=pod |
176 |
# 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 './' |
189 |
if ($basedir) { |
if ($basedir) { |
190 |
-e "$basedir/$executable" or die("$basedir/$executable does not exist."); |
-e "$basedir/$executable" or die("$basedir/$executable does not exist."); |
191 |
$basedir .= '/'; |
$basedir .= '/'; |
192 |
|
$cmd = "$basedir$cmd"; |
193 |
} elsif ($use_path) { |
} elsif ($use_path) { |
194 |
$basedir = ""; |
#$basedir = ""; |
195 |
|
$basedir = findpath($executable); |
196 |
|
#print "basedir: $basedir", "\n"; |
197 |
|
my $abspath = File::Spec->catfile($basedir, $executable); |
198 |
|
#print STDOUT "PATH: ", $abspath, "\n"; |
199 |
|
-e $abspath or die("$abspath does not exist."); |
200 |
|
$cmd = $abspath . ' ' . $executable_args; |
201 |
} else { |
} else { |
202 |
-e $executable or die("$executable does not exist."); |
-e $executable or die("$executable does not exist."); |
203 |
#$basedir = "."; |
#$basedir = "."; |
204 |
#$basedir .= './'; |
#$basedir .= './'; |
205 |
$basedir = ""; |
$basedir = ""; |
206 |
|
$cmd = "$basedir$cmd"; |
207 |
} |
} |
|
$cmd = "$basedir$cmd"; |
|
208 |
|
|
209 |
# V1 - backticks or qq{} |
# V1 - backticks or qq{} |
210 |
#`$cmd`; |
#`$cmd`; |
213 |
# V2 - via 'system' |
# V2 - via 'system' |
214 |
#system($cmd); |
#system($cmd); |
215 |
|
|
216 |
if (not $use_path) { |
#if (not $use_path) { |
217 |
my $application = get_executable($cmd); |
my $application = get_executable($cmd); |
218 |
$cmd = "$application$cmd" if $application; |
$cmd = "$application$cmd" if $application; |
219 |
} |
#} |
220 |
|
|
221 |
|
my @cmd = split(' ', $cmd); |
222 |
|
|
223 |
# V3 - using IPC (optional) |
# V3 - using IPC (optional) |
224 |
if ($options->{async}) { |
if ($options->{async}) { |
225 |
|
|
|
#$cmd = "$application $cmd" if $application; |
|
|
#my $application = get_executable_wrapper($cmd); |
|
|
|
|
|
print STDERR "run_cmd: IPC::Session::NoShell: $cmd", "\n"; |
|
226 |
#run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; |
#run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; |
227 |
|
|
228 |
my @cmd = split(' ', $cmd); |
print STDOUT "run_cmd[async]: Proc::Background: $cmd", "\n"; |
|
|
|
229 |
|
|
230 |
# V3.1 - using IPC::Run |
# V3.1 - using IPC::Run |
231 |
# |
# |
232 |
# tests: |
# tests: |
|
#my $in; my $out; my $err; |
|
|
#print "IPC::Run: $cmd", "\n"; |
|
|
#start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
|
|
# |
|
|
# success on Win32, but seems broken at 'timeout' on linux: |
|
|
#run(\@cmd, timeout(2)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
|
|
|
|
|
# other tests ;) |
|
|
#$IPC::Run::Timer::timeout = 2000; |
|
|
#start $cmd or die("IPC::Run could not start '$cmd'."); |
|
|
|
|
|
|
|
|
# V3.2 - using IPC::Session |
|
|
# success on Linux AND Win32 ?? |
|
|
# |
|
|
# set timeout: |
|
|
# (don't really know why we needs 2 secconds |
|
|
# to wait for init of process !?!) |
|
|
my $session_timeout = 2; |
|
|
# set session name (default: cmd as string): |
|
|
my $session_command = $cmd; |
|
|
# create session: |
|
|
my $session = new IPC::Session::NoShell($session_command, $session_timeout); |
|
233 |
|
|
234 |
# send 'cmd' to session - not required since complete command is sent via constructor above |
if (RUNNING_IN_HELL()) { |
|
#$session->send(\@cmd); |
|
235 |
|
|
236 |
# optional switch case: |
#my $in; my $out; my $err; |
237 |
#for ($session->stdout()) { |
|
238 |
#} |
#print STDOUT "findpath: ", findpath('rap.pl'), "\n"; |
239 |
# optional get error: |
|
240 |
#my $err = session->stderr(); |
# no success! |
241 |
|
#start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
242 |
|
# |
243 |
|
# success on Win32, but seems broken at 'timeout' on linux: |
244 |
|
#run(\@cmd, timeout(3)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
245 |
|
#start(\@cmd, timer(2)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
246 |
|
#start(\@cmd, \undef) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
247 |
|
#start \@cmd or croak("run_cmd: IPC::Run could not start '$cmd'."); |
248 |
|
#start(\@cmd, timeout(1)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
249 |
|
|
250 |
|
# other tests ;) |
251 |
|
#$IPC::Run::Timer::timeout = 2000; |
252 |
|
#start $cmd or die("IPC::Run could not start '$cmd'."); |
253 |
|
|
254 |
|
#my $in; my $out; my $t; |
255 |
|
#my $harness = start( \@cmd, \$in, \$out, $t = timer( 5 ) ) ; |
256 |
|
#my $harness = start( \@cmd ) ; |
257 |
|
#$harness->pump_nb(); |
258 |
|
|
259 |
|
#my $postfix = '2>&1 |'; |
260 |
|
#open PIPE, "$cmd $postfix" or die("run_cmd: could not run in background via open!"); |
261 |
|
|
262 |
|
my $proc1 = Proc::Background->new(@cmd); |
263 |
|
print "pid: ", $proc1->pid(), "\n"; |
264 |
|
|
265 |
|
|
266 |
|
} else { |
267 |
|
|
268 |
|
#print STDOUT "run_cmd: IPC::Session: $cmd", "\n"; |
269 |
|
|
270 |
|
# V3.2 - using IPC::Session |
271 |
|
# success on Linux AND Win32 ?? |
272 |
|
# |
273 |
|
# set timeout: |
274 |
|
# (don't really know why we needs some secconds |
275 |
|
# to wait for init of process !?!) |
276 |
|
#my $session_timeout = 15; |
277 |
|
# set session name (default: cmd as string): |
278 |
|
#my $session_command = $cmd; |
279 |
|
#my $session_shell = "/bin/sh"; |
280 |
|
# create session (beware of using '->new' here!?): |
281 |
|
#my $session = new IPC::Session($session_shell, $session_timeout); |
282 |
|
|
283 |
|
# send 'cmd' to session - not required since complete command is sent via constructor above |
284 |
|
#$session->send($cmd); |
285 |
|
|
286 |
|
#my $output = $session->stdout(); |
287 |
|
#print "WS::Admin started.\n" if $output = ' '; |
288 |
|
|
289 |
|
# tests |
290 |
|
#$session->send("echo hello"); |
291 |
|
#chomp(my $hello = $session->stdout()); |
292 |
|
#print "ok 3\n" if $hello eq "hello"; |
293 |
|
|
294 |
|
# optional switch case: |
295 |
|
#for ($session->stdout()) { |
296 |
|
# /_bootDataBases/ && do { print "WS::Admin started.\n" }; |
297 |
|
#} |
298 |
|
# optional get error: |
299 |
|
#my $err = session->stderr(); |
300 |
|
#print "ERR: " . Dumper($err) . "\n"; |
301 |
|
|
302 |
|
#open( *OUT, ">out.txt" ) ; |
303 |
|
#open( *ERR, ">err.txt" ) ; |
304 |
|
#run(\@cmd, \undef, \*OUT, \*ERR ) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
305 |
|
#my ($out, $err); |
306 |
|
#my $h = start(\@cmd, \undef, \*OUT, \*ERR ) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
307 |
|
#finish $h if $err =~ /error/; |
308 |
|
|
309 |
|
# get child pid |
310 |
|
#my $kid; |
311 |
|
#do { $kid=wait(); } until $kid > 0; |
312 |
|
#print "Child PID: " . $kid . "n"; |
313 |
|
|
314 |
|
#finish $h; |
315 |
|
|
316 |
|
|
317 |
|
# test using Proc::Background - success !! |
318 |
|
my $proc = Proc::Background->new($cmd); |
319 |
|
my $kid = $proc->pid(); |
320 |
|
print STDOUT "run_cmd: Proc::Background: $cmd, child PID $kid", "\n"; |
321 |
|
$proc->wait(); |
322 |
|
$proc->die(); |
323 |
|
|
324 |
|
} |
325 |
|
|
326 |
|
|
327 |
|
} elsif ($options->{detach}) { |
328 |
|
|
329 |
|
if (RUNNING_IN_HELL()) { |
330 |
|
print STDOUT "run_cmd[detach]: Proc::Background: $cmd", "\n"; |
331 |
|
my $proc1 = Proc::Background->new(@cmd); |
332 |
|
print "pid: ", $proc1->pid(), "\n"; |
333 |
|
} else { |
334 |
|
print STDOUT "run_cmd[detach]: system('$cmd' &).", "\n"; |
335 |
|
system($cmd . ' &'); |
336 |
|
} |
337 |
|
|
|
|
|
338 |
} else { |
} else { |
339 |
print STDERR "run_cmd: system('$cmd').", "\n"; |
print STDOUT "run_cmd: system('$cmd').", "\n"; |
340 |
system($cmd); |
system($cmd); |
341 |
} |
} |
342 |
|
|
343 |
print STDERR "run_cmd: ready.", "\n"; |
print STDOUT "run_cmd: ready.", "\n"; |
344 |
|
|
345 |
} |
} |
346 |
|
|
393 |
return $guid; |
return $guid; |
394 |
} |
} |
395 |
|
|
396 |
|
# [modified] from: http://www.mit.edu/afs/athena/contrib/watchmaker/src/pt/Configure |
397 |
|
sub findpath { |
398 |
|
#local($path); |
399 |
|
#local($arg) = shift; |
400 |
|
my $arg = shift; |
401 |
|
my $path; |
402 |
|
#for my $dir (split(/:/,$ENV{'PATH'})) { |
403 |
|
for my $dir (File::Spec->path()) { |
404 |
|
#if (-x "$dir/$arg" && -f _) { |
405 |
|
#print STDOUT "scanning: ", "$dir/$arg", "\n"; |
406 |
|
if (-e "$dir/$arg") { |
407 |
|
#$path = "$dir/$arg"; |
408 |
|
#$path = "$dir/"; |
409 |
|
$path = $dir; |
410 |
|
last; |
411 |
|
} |
412 |
|
} |
413 |
|
$path; |
414 |
|
} |
415 |
|
|
416 |
1; |
1; |
417 |
__END__ |
__END__ |