windows 来自 PHP exec 的 taskkill
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1853625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
taskkill from PHP exec
提问by Abs
I have just tried to execute this:
我刚刚尝试执行此操作:
function kill_hr(){
exec("taskkill /IM uper.exe", $output = array(), $return);
print_r($output);
echo "<br />".$return;
}
However, the output is this and its not very useful:
但是,输出是这样的,它不是很有用:
Array ( ) 1
数组 ( ) 1
When the process doesn't exist its this:
当进程不存在时,它是这样的:
Array ( ) 128
数组 ( ) 128
I am trying to work out why it gives me a 1 when the process exists - is this a permissions problem? If so, how can I rectify this?
我试图弄清楚为什么当进程存在时它给我一个 1 - 这是一个权限问题吗?如果是这样,我该如何纠正?
回答by Abs
Thank you all for your help - I finally managed to kill the process using this:
谢谢大家的帮助 - 我终于设法用这个来终止进程:
$output = shell_exec('taskkill /F /IM "uper.exe"');
I am sure it will work with exec as well, but the important part is the /F
forcing it! :)
我相信它也可以与 exec 一起使用,但重要的部分是/F
强制它!:)
回答by Pekka
In addition to the other answers, the numeric value returned by the command is called the ERRORLEVEL in Windows. When playing around with taskkill on the command line, you can make the last errorlevel returned visible using
除了其他答案外,该命令返回的数值在 Windows 中称为 ERRORLEVEL。在命令行上使用 taskkill 时,您可以使用以下命令使返回的最后一个错误级别可见
echo %ERRORLEVEL%
回答by leepowers
I've had this problem before - a few things what worked in the past:
我以前遇到过这个问题 - 一些过去有效的事情:
Solution #1
解决方案#1
Launch taskkill
with the Windows start
command:
taskkill
使用 Windowsstart
命令启动:
exec('start /B taskkill /IM notepad.exe', $output = array(), $return);
exec('start /B taskkill /IM notepad.exe', $output = array(), $return);
Solution #2
解决方案#2
Open a new command-line using cmd.exe
:
使用cmd.exe
以下命令打开一个新命令行:
exec('cmd /c taskkill /IM notepad.exe', $output = array(), $return);
exec('cmd /c taskkill /IM notepad.exe', $output = array(), $return);
**Note*: I've used both methods in the past to launch background processes from PHP - I'm not sure what the return values will be, so you'll need to experiment.
**注意*:我过去曾使用这两种方法从 PHP 启动后台进程 - 我不确定返回值是什么,因此您需要进行试验。
回答by Vegard Larsen
The $return
value is the value the program returns. In Windows, the return values 0 and 1 is usually used to indicate success, so you can easily tell when the termination was successfull.
该$return
值是程序返回的值。在 Windows 中,返回值 0 和 1 通常用于表示成功,因此您可以轻松判断终止何时成功。
However, a return value of 128 is arbitrary, meaning that the program developers of taskkill
decided on it themselves. 128 likely means that the process doesn't exist.
但是,返回值 128 是任意的,这意味着程序开发人员taskkill
自己决定。128 可能意味着该进程不存在。
There does not seem to be any documentation that documents the return values of taskkill
, unfortunately.
taskkill
不幸的是,似乎没有任何文档记录 的返回值。
If your goal is to prevent uper.exe
from being present, a return value of 128 and 1 would then both be acceptable, and your code becomes:
如果您的目标是防止uper.exe
出现,则返回值 128 和 1 都是可以接受的,并且您的代码变为:
function kill_hr()
{
exec("taskkill /IM uper.exe", $output = array(), $return);
return $return == 1 || $return == 128;
}
The function will return true
if uper.exe
was successfully terminated, or if it wasn't running in the first place.
true
如果uper.exe
成功终止,或者它最初没有运行,该函数将返回。
Edit: Re-reading your post, you can try the following; use runas
to start a command prompt as your web server user (from an administrative command prompt):
编辑:重新阅读您的帖子,您可以尝试以下操作;用于runas
以您的 Web 服务器用户身份启动命令提示符(从管理命令提示符):
runas /user:account@machine cmd
Then you will have a command prompt running as your web server, and you can issue the taskkill
command from there. Then you will likely see a textual error message.
然后您将有一个命令提示符作为您的 Web 服务器运行,您可以taskkill
从那里发出命令。然后您可能会看到文本错误消息。