简体   繁体   中英

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

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");

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM