Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Starting and killing a process

Status
Not open for further replies.

Vovin

Programmer
Aug 24, 2003
63
GB
Hi,

can anyone help me with this? I'm trying to start a small C program (foo.exe) from my perl program.

Once started the C program runs forever monitoring a directory for incoming files. I want to start this process but stop it when it outputs the following line to the screen:

'Waiting for files to arrive..'

How can start this process, monitor it's output, then kill it? Any help would be much appreciated.

Thanks in advance.
 
What about forking a perl process and then using "exec" to transform your perl process to a C process. The fork will return the pid of the child process to the parent.
In Unix it's easy then to kill a process but pic but I don't know wo much about windoze.
As for watching it's output, redirect the output to a file and use the perl parent to read it.


Trojan.
 
Thnaks Trojan,

I tried to do this using the following code which is similar to some I found in O'Reilly's 'Programming Perl' pg 427 (3rd Edition). I couldn't get it to work though. Can anyone tell me why this doesn't work? I'll have a go at using the exec command too:

sub startFoo
{
my $currentWorkingDirectory = getcwd();
chdir("/foo");

open BAR, "foo -l -v|" || die "can't fork: $!";
while (<BAR>)
{
if (/.*/)
{
last;
}
}

close PRINT_MAN or die "Unable to close $! $?";

chdir($currentWorkingDirectory);
}

 
First obvious issue is that you're opening "BAR" and closing "PRINT_MAN".
Secondly, I don't know if closing the pipeline would kill the process.
But it was a solution I had thought of.
You could try it but you'd be limited to only one child and the parent would have to monitor it then with that while loop.
In your case the /.*/ should be /Waiting for files to arrive/.
Does your process live in a folder called "/foo"? If not, chdir to the appropriate place first or run the process with a full path.


Trojan.
 
Yeah I think your right - closing the pipeline doesn't seem to close the pipeline. Does anyone have any code to do this. I'm not really getting anywhere fast with this?How would I get the ID for this process so that I can kill it?
 
I did give you a suggestion earlier.
Fork a child to get it's ip and exec your .exe in the child
The child then becomes the .exe and the parent has it's pid (process ID).
If you need to parse the stdout of the child as your initial request suggests then pipe the output of the child to a file and read the file with normal perl fileIO.
Have you tried this yet?
I don't run windoze so I can't test it in your environment.


Trojan.
 
If TWB's method doesn't work you can try the Win32::process module. This example will open notepad, then kill it after 4 seconds
Code:
use Win32::Process;
use Win32;

sub ErrorReport{
print Win32::FormatMessage( Win32::GetLastError() );
}

Win32::Process::Create(my $ProcessObj,
                            "C:\\windows\\system32\\notepad.exe",
                            "notepad",
                            0,
                            NORMAL_PRIORITY_CLASS,
                            ".")|| die ErrorReport();

sleep(4);
$ProcessObj->Kill(0);

If you want the PID, you can get it with this method:
my $pid = $ProcessObj->GetProcessID();

check out the docs for details
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top