[英]PHP - How to keep a Command Shell open for executing multiple commands?
嗨,我試圖在Windows計算機上使用PHP執行多個命令。 我嘗試了這段代碼:
<?php
$output= shell_exec("powershell.exe");
$output2= shell_exec("write-host gokul");
shell_exec("exit");
echo( '<pre>' );
echo( $output );
echo( $output2 );
echo( '</pre>' );
?>
PHP使用新的shell執行每個命令,並在完成后將其關閉!
我知道每個'shell_exec'都會創建一個新的shell,並且控件會等到命令執行完成。 如何創建在關閉前一直在背景中打開的外殼 ?
不確定'proc_open'或'popen'是否可以提供幫助?
當我嘗試PROC_OPEN()時更新
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "error.txt", "a") // stderr is a file to write to
);
$process = proc_open('cmd.exe', $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], 'dir');
fwrite($pipes[0], 'powershell.exe');
fwrite($pipes[0], 'write-host gokul');
fclose($pipes[0]);
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($process);
}
它僅打印提示。 我真的可以使用proc_open執行多個命令嗎? 如果是這樣,怎么辦?
Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\wamp\bin\apache\Apache2.2.17>More?
使用proc_open
是正確的嘗試,但是您需要用proc_open
結束每個命令。 就像您在shell會話中手動鍵入命令一樣。
它看起來應該像這樣:
fwrite($pipes[0], 'dir' . PHP_EOL);
fwrite($pipes[0], 'powershell.exe' . PHP_EOL);
fwrite($pipes[0], 'write-host gokul' . PHP_EOL);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.