maryam yousefi
maryam yousefi

Reputation: 81

Unable to kill a process in Perl

I wrote a code to run an exe file as follow:

 #!/usr/local/bin/perl     
 use Cwd;                      
 $directory   = 'e:/USER/';
 chdir($directory) or die ; 
 system("Bnc25-Windows.exe -nw");

Now I want to write another code to stop it. I wrote:

 #!/usr/local/bin/perl     
 use Cwd;                      
 $directory   = 'e:/USER/';
 chdir($directory) or die ; 
 kill Bnc25-Windows.exe ; 

but it doesn't work and I see in task manager window that the exe file is running. I don't really know where is the problem. thanks for any help

Upvotes: 2

Views: 2098

Answers (3)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57640

You are using Windows. There is no kill command in Windows. You can use taskkill for this.

Use the system function again.

system("taskkill /im Bnc25-Windows.exe /f");

Upvotes: 5

Ilmari Karonen
Ilmari Karonen

Reputation: 50328

The Perl kill function needs (a signal name/number and) the numeric ID of the process you want to kill, not its name.

As general advice, I would strongly recommend beginning your code with:

use strict;
use warnings;

and fixing any errors and warnings they generate.

For example, if you had done that with the code in your question, you would've (after fixing the missing quotes around Bnc25-Windows.exe and the missing my before the first declaration of $directory, so that the code passes strict checks) received the following warning:

Unrecognized signal name "Bnc25-Windows.exe" at test.pl line 7.

This would've told you that kill is trying to parse "Bnc25-Windows.exe" as a signal name, which would've suggested that there's something wrong with the way you're trying to use it, and would hopefully have led you to look at the documentation (see link above), which both describes the proper way to use the kill function in Perl, and also links to the portability warnings about using it on non-Unix systems.

Upvotes: 4

sds
sds

Reputation: 60004

kill kills processes by PID. You need killall to kill a process by executable name. Both are unix commands, available on via cygwin (which you are, presumably, using).

Upvotes: 0

Related Questions