Update: Based on your comments, I get the feeling that your programs are not examples of best practices on Linux or Windows. However, I am sure, when reading the documentation for Win32::Process
, you noticed that you can call the Kill
method on the process to terminate it. So, I changed the example below to do that.
Your chances of getting useful help increase exponentially if you provide real code. Here are the arguments to Win32::Process::Create
:
$iflags
: flag: inherit calling processes handles or not
Now, I am not sure if you are trying to capture the STDOUT
of the second process or if you are trying to have its STDOUT
output show up in the same console as the parent.
If the latter, then the following scripts illustrate one way of doing that:
parent.pl
#!/usr/bin/perl
use strict;
use warnings;
use Win32;
use Win32::Process;
$| = 1;
my $p;
print "Starting child process ... \n";
Win32::Process::Create(
$p,
'c:/opt/perl/bin/perl.exe',
'perl hello.pl',
1,
NORMAL_PRIORITY_CLASS,
'.',
) or die Win32::FormatMessage( Win32::GetLastError() );
print "Waiting three seconds before killing 'hello.pl'\n";
for (1 .. 3) {
print;
sleep 1;
}
$p->Kill(0)
or die "Cannot kill '$p'";
hello.pl
#!/usr/bin/perl
$| = 1;
print "Hello World\n";
print "Sleeping 1000 seconds\n";
for (1 .. 1000) {
sleep 1;
print '.';
}
Output:
Starting child process ...
Waiting three seconds before killing 'hello.pl'
1Hello World
Sleeping 1000 seconds
2.3.
Now, I am still not sure why you are using Win32::Process
. Unless there is a specific reason to tie your script to Win32
, I would recommend using standard Perl facilities. For example, read perldoc -f open
and perldoc perlipc
(see esp. Using open for IPC).
Explain your question better to get answers that address your particular situation rather than generalities.