| 2 | ##  $Id$ | ##  $Id$ | 
| 3 | ## --------------------------------------------------------------------------- | ## --------------------------------------------------------------------------- | 
| 4 | ##  $Log$ | ##  $Log$ | 
| 5 |  | ##  Revision 1.18  2003/12/05 04:58:04  joko | 
| 6 |  | ##  + minor update: doesn't require IPC::Session anymore | 
| 7 |  | ## | 
| 8 |  | ##  Revision 1.17  2003/07/02 11:17:32  jonen | 
| 9 |  | ##  minor changes | 
| 10 |  | ## | 
| 11 |  | ##  Revision 1.16  2003/06/25 22:49:56  joko | 
| 12 |  | ##  RUNNING_IN_HELL mode for detach option | 
| 13 |  | ## | 
| 14 |  | ##  Revision 1.15  2003/06/24 20:21:12  jonen | 
| 15 |  | ##  + changed linux part of run_cmd to use Proc::Background instead of IPC::... | 
| 16 |  | ## | 
| 17 | ##  Revision 1.14  2003/06/24 20:13:18  joko | ##  Revision 1.14  2003/06/24 20:13:18  joko | 
| 18 | ##  + sub findpatch | ##  + sub findpatch | 
| 19 | ##  + now using findpatch and Proc::Background for win32/perl | ##  + now using findpatch and Proc::Background for win32/perl | 
| 87 | use Carp; | use Carp; | 
| 88 |  |  | 
| 89 | # NEW - 2003-06-23 for Linux (what about *BSD?) | # NEW - 2003-06-23 for Linux (what about *BSD?) | 
| 90 | use IPC::Session; | #use IPC::Session; | 
| 91 |  |  | 
| 92 | use File::Spec; | use File::Spec; | 
| 93 | use Proc::Background; | use Proc::Background; | 
| 221 | $cmd = "$application$cmd" if $application; | $cmd = "$application$cmd" if $application; | 
| 222 | #} | #} | 
| 223 |  |  | 
| 224 |  | my @cmd = split(' ', $cmd); | 
| 225 |  |  | 
| 226 | # V3 - using IPC (optional) | # V3 - using IPC (optional) | 
| 227 | if ($options->{async}) { | if ($options->{async}) { | 
| 228 |  |  | 
| 229 | #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; | #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; | 
| 230 | my @cmd = split(' ', $cmd); |  | 
| 231 |  | print STDOUT "run_cmd[async]: Proc::Background: $cmd", "\n"; | 
| 232 |  |  | 
| 233 | # V3.1 - using IPC::Run | # V3.1 - using IPC::Run | 
| 234 | # | # | 
| 237 | if (RUNNING_IN_HELL()) { | if (RUNNING_IN_HELL()) { | 
| 238 |  |  | 
| 239 | #my $in; my $out; my $err; | #my $in; my $out; my $err; | 
|  | print STDOUT "run_cmd: IPC::Run: $cmd", "\n"; |  | 
| 240 |  |  | 
| 241 | #print STDOUT "findpath: ", findpath('rap.pl'), "\n"; | #print STDOUT "findpath: ", findpath('rap.pl'), "\n"; | 
| 242 |  |  | 
| 268 |  |  | 
| 269 | } else { | } else { | 
| 270 |  |  | 
| 271 | print STDOUT "run_cmd: IPC::Session: $cmd", "\n"; | #print STDOUT "run_cmd: IPC::Session: $cmd", "\n"; | 
| 272 |  |  | 
| 273 | # V3.2 - using IPC::Session | # V3.2 - using IPC::Session | 
| 274 | #  success on Linux AND Win32 ?? | #  success on Linux AND Win32 ?? | 
| 275 | # | # | 
| 276 | # set timeout: | # set timeout: | 
| 277 | #  (don't really know why we needs 2 secconds | #  (don't really know why we needs some secconds | 
| 278 | #   to wait for init of process !?!) | #   to wait for init of process !?!) | 
| 279 | my $session_timeout = 3; | #my $session_timeout = 15; | 
| 280 | # set session name (default: cmd as string): | # set session name (default: cmd as string): | 
| 281 | my $session_name = $cmd; | #my $session_command = $cmd; | 
| 282 | # create session: | #my $session_shell = "/bin/sh"; | 
| 283 | my $session = IPC::Session->new($session_name, $session_timeout); | # create session (beware of using '->new' here!?): | 
| 284 |  | #my $session = new IPC::Session($session_shell, $session_timeout); | 
| 285 |  |  | 
| 286 | # 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 | 
| 287 | $session->send(\@cmd); | #$session->send($cmd); | 
| 288 |  |  | 
| 289 | #print $session->stdout(), "\n"; | #my $output = $session->stdout(); | 
| 290 |  | #print "WS::Admin started.\n" if $output = ' '; | 
| 291 |  |  | 
| 292 |  | # tests | 
| 293 |  | #$session->send("echo hello"); | 
| 294 |  | #chomp(my $hello = $session->stdout()); | 
| 295 |  | #print "ok 3\n" if $hello eq "hello"; | 
| 296 |  |  | 
| 297 | # optional switch case: | # optional switch case: | 
| 298 | #for ($session->stdout()) { | #for ($session->stdout()) { | 
| 299 |  | #  /_bootDataBases/ && do { print "WS::Admin started.\n" }; | 
| 300 | #} | #} | 
| 301 | # optional get error: | # optional get error: | 
| 302 | #my $err = session->stderr(); | #my $err = session->stderr(); | 
| 303 |  | #print "ERR: " . Dumper($err) . "\n"; | 
| 304 |  |  | 
| 305 |  | #open( *OUT, ">out.txt" ) ; | 
| 306 |  | #open( *ERR, ">err.txt" ) ; | 
| 307 |  | #run(\@cmd, \undef,  \*OUT, \*ERR  ) or croak("run_cmd: IPC::Run could not start '$cmd'."); | 
| 308 |  | #my ($out, $err); | 
| 309 |  | #my $h = start(\@cmd, \undef,  \*OUT, \*ERR  ) or croak("run_cmd: IPC::Run could not start '$cmd'."); | 
| 310 |  | #finish $h if $err =~ /error/; | 
| 311 |  |  | 
| 312 |  | # get child pid | 
| 313 |  | #my $kid; | 
| 314 |  | #do { $kid=wait(); } until $kid > 0; | 
| 315 |  | #print "Child PID: " . $kid . "n"; | 
| 316 |  |  | 
| 317 |  | #finish $h; | 
| 318 |  |  | 
| 319 |  |  | 
| 320 |  | # test using Proc::Background - success !! | 
| 321 |  | my $proc = Proc::Background->new($cmd); | 
| 322 |  | my $kid = $proc->pid(); | 
| 323 |  | print STDOUT "run_cmd: Proc::Background: $cmd, child PID $kid", "\n"; | 
| 324 |  | $proc->wait(); | 
| 325 |  | $proc->die(); | 
| 326 |  |  | 
| 327 | } | } | 
| 328 |  |  | 
| 329 |  |  | 
| 330 |  | } elsif ($options->{detach}) { | 
| 331 |  |  | 
| 332 |  | if (RUNNING_IN_HELL()) { | 
| 333 |  | print STDOUT "run_cmd[detach]: Proc::Background: $cmd", "\n"; | 
| 334 |  | my $proc1 = Proc::Background->new(@cmd); | 
| 335 |  | print "pid: ", $proc1->pid(), "\n"; | 
| 336 |  | } else { | 
| 337 |  | print STDOUT "run_cmd[detach]: system('$cmd' &).", "\n"; | 
| 338 |  | system($cmd . ' &'); | 
| 339 |  | } | 
| 340 |  |  | 
| 341 | } else { | } else { | 
| 342 | print STDOUT "run_cmd: system('$cmd').", "\n"; | print STDOUT "run_cmd: system('$cmd').", "\n"; | 
| 343 |  | #print $ENV{PERL5LIB}, "\n"; | 
| 344 | system($cmd); | system($cmd); | 
| 345 | } | } | 
| 346 |  |  |