简体   繁体   English

无法杀死Perl中的进程

[英]Unable to kill a process in Perl

I wrote a code to run an exe file as follow: 我编写了一个运行exe文件的代码如下:

 #!/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. 但它不起作用,我在任务管理器窗口中看到exe文件正在运行。 I don't really know where is the problem. 我真的不知道问题出在哪里。 thanks for any help 谢谢你的帮助

You are using Windows. 您正在使用Windows。 There is no kill command in Windows. Windows中没有kill命令。 You can use taskkill for this. 你可以使用taskkill

Use the system function again. 再次使用system功能。

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. Perl kill函数需要(信号名称/编号和)要杀死的进程的数字ID,而不是其名称。

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: 例如,如果您已经使用问题中的代码完成了这项操作,那么(在修复了Bnc25-Windows.exe周围的缺失引号并在第一次声明$directory之前丢失了my的代码之后),以便代码通过strict检查)收到以下警告:

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正试图解析"Bnc25-Windows.exe"作为信号名称,这会暗示你尝试使用它的方式有问题,并希望能引导你查看文档(参见上面的链接),它们都描述了在Perl中使用kill函数的正确方法,还链接到在非Unix系统上使用它的可移植性警告

kill kills processes by PID . kill通过杀死过程PID You need killall to kill a process by executable name. 你需要killall来杀死一个可执行名称的进程。 Both are unix commands, available on via cygwin (which you are, presumably, using). 两者都是unix命令,可以通过cygwin (你可能会使用它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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