--- nfo/perl/libs/shortcuts.pm 2003/06/24 20:13:18 1.14 +++ nfo/perl/libs/shortcuts.pm 2004/06/21 14:13:21 1.20 @@ -1,7 +1,25 @@ ## --------------------------------------------------------------------------- -## $Id: shortcuts.pm,v 1.14 2003/06/24 20:13:18 joko Exp $ +## $Id: shortcuts.pm,v 1.20 2004/06/21 14:13:21 jonen Exp $ ## --------------------------------------------------------------------------- ## $Log: shortcuts.pm,v $ +## Revision 1.20 2004/06/21 14:13:21 jonen +## added helper function +## +## Revision 1.19 2004/05/11 19:45:30 joko +## now exporting RUNNING_IN_HELL +## +## Revision 1.18 2003/12/05 04:58:04 joko +## + minor update: doesn't require IPC::Session anymore +## +## Revision 1.17 2003/07/02 11:17:32 jonen +## minor changes +## +## Revision 1.16 2003/06/25 22:49:56 joko +## RUNNING_IN_HELL mode for detach option +## +## Revision 1.15 2003/06/24 20:21:12 jonen +## + changed linux part of run_cmd to use Proc::Background instead of IPC::... +## ## Revision 1.14 2003/06/24 20:13:18 joko ## + sub findpatch ## + now using findpatch and Proc::Background for win32/perl @@ -65,6 +83,8 @@ get_chomped bool2status make_guid + RUNNING_IN_HELL + RUNNING_IN_HEAVEN ); @@ -75,7 +95,7 @@ use Carp; # NEW - 2003-06-23 for Linux (what about *BSD?) -use IPC::Session; +#use IPC::Session; use File::Spec; use Proc::Background; @@ -96,6 +116,7 @@ } sub RUNNING_IN_HELL () { $^O eq 'MSWin32' } +sub RUNNING_IN_HEAVEN () { $^O =~ m/bsd/ } sub get_executable { @@ -209,12 +230,14 @@ $cmd = "$application$cmd" if $application; #} + my @cmd = split(' ', $cmd); + # V3 - using IPC (optional) if ($options->{async}) { #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; - my @cmd = split(' ', $cmd); - + + print STDOUT "run_cmd[async]: Proc::Background: $cmd", "\n"; # V3.1 - using IPC::Run # @@ -223,7 +246,6 @@ if (RUNNING_IN_HELL()) { #my $in; my $out; my $err; - print STDOUT "run_cmd: IPC::Run: $cmd", "\n"; #print STDOUT "findpath: ", findpath('rap.pl'), "\n"; @@ -255,34 +277,79 @@ } else { - print STDOUT "run_cmd: IPC::Session: $cmd", "\n"; + #print STDOUT "run_cmd: IPC::Session: $cmd", "\n"; # V3.2 - using IPC::Session # success on Linux AND Win32 ?? # # set timeout: - # (don't really know why we needs 2 secconds + # (don't really know why we needs some secconds # to wait for init of process !?!) - my $session_timeout = 3; + #my $session_timeout = 15; # set session name (default: cmd as string): - my $session_name = $cmd; - # create session: - my $session = IPC::Session->new($session_name, $session_timeout); + #my $session_command = $cmd; + #my $session_shell = "/bin/sh"; + # create session (beware of using '->new' here!?): + #my $session = new IPC::Session($session_shell, $session_timeout); # send 'cmd' to session - not required since complete command is sent via constructor above - $session->send(\@cmd); - - #print $session->stdout(), "\n"; + #$session->send($cmd); + + #my $output = $session->stdout(); + #print "WS::Admin started.\n" if $output = ' '; + + # tests + #$session->send("echo hello"); + #chomp(my $hello = $session->stdout()); + #print "ok 3\n" if $hello eq "hello"; # optional switch case: #for ($session->stdout()) { + # /_bootDataBases/ && do { print "WS::Admin started.\n" }; #} # optional get error: #my $err = session->stderr(); + #print "ERR: " . Dumper($err) . "\n"; + + #open( *OUT, ">out.txt" ) ; + #open( *ERR, ">err.txt" ) ; + #run(\@cmd, \undef, \*OUT, \*ERR ) or croak("run_cmd: IPC::Run could not start '$cmd'."); + #my ($out, $err); + #my $h = start(\@cmd, \undef, \*OUT, \*ERR ) or croak("run_cmd: IPC::Run could not start '$cmd'."); + #finish $h if $err =~ /error/; + + # get child pid + #my $kid; + #do { $kid=wait(); } until $kid > 0; + #print "Child PID: " . $kid . "n"; + + #finish $h; + + + # test using Proc::Background - success !! + my $proc = Proc::Background->new($cmd); + my $kid = $proc->pid(); + print STDOUT "run_cmd: Proc::Background: $cmd, child PID $kid", "\n"; + $proc->wait(); + $proc->die(); + } - + + + } elsif ($options->{detach}) { + + if (RUNNING_IN_HELL()) { + print STDOUT "run_cmd[detach]: Proc::Background: $cmd", "\n"; + my $proc1 = Proc::Background->new(@cmd); + print "pid: ", $proc1->pid(), "\n"; + } else { + print STDOUT "run_cmd[detach]: system('$cmd' &).", "\n"; + system($cmd . ' &'); + } + } else { print STDOUT "run_cmd: system('$cmd').", "\n"; + #print $ENV{PERL5LIB}, "\n"; system($cmd); }