如何 运行 并在 windows 上忘记 php 中的命令

How to run and forget a command in php on windows

我想要做的是 运行 在 Windows 一个命令上,该命令使用特定参数启动 putty.exe,这将启动一个连接并且 运行一些命令,在其他服务器的 putty 会话中指定的 txt 文件中。我只需要从 linux 上的其他服务器 运行 一些脚本,而这个主脚本将 运行 在 windows 服务器上。

看起来像这样:

$putty_exe_path = 'C:\putty\putty.exe';
$linux_command_txt_file_path = 'C:\putty\commands_to_run_on_linux.txt';
$putty_session_name = 'puttysessionname';
$linux_user = 'root'; 
$linux_password = 'password';

$cmd = "\"\"".$putty_exe_path."\" -load ".$putty_session_name." -l ".$linux_user." -pw ".$linux_password." -m \"".$linux_command_txt_file_path."\"\"";             

shell_exec($cmd);  

它可以工作,但是当 linux 上的脚本出现问题时,我 运行 使用这种方式 运行ning 了很长时间,因为我的脚本正在等待用于输出。我想 运行 使用 exec()/shell_exec() 或以某种方式在 windows 服务器上执行该命令并退出。我不需要输出。我需要解雇并忘记。我一直在寻找解决方案,但我发现的只是这个:

> /dev/null 2>/dev/null &

但我猜它在 windows 上不起作用。

不是答案,但评论时间太长

了解 > /dev/null 2>/dev/null & 在做什么很重要。

在 Linux/Unix 操作系统中,/dev/null 是 "nothing" 的模拟设备,它会丢弃写入其中的任何内容。它 /dev/null 在 Windows 中不存在。在此处进行更多研究:https://en.wikipedia.org/wiki/Null_device

在 linux 中,当 运行 命令时,有两个主要的输出流,stdout 和 stderr。 >/dev/null 将 stdout 重定向到 /dev/null,2>/dev/null 将 stderr 重定向到 /dev/null,& 将命令发送到后台。现在您可以使用它来查找 Windows.

中的等效项

如果您想在 windows 中执行控制台命令并且不等待其输出,您可以将输出重定向到 > NUL

例如,在 php 中执行:

var_dump(system("cmd.exe > NUL"));// will output inmediately string(0)""
var_dump(system("cmd.exe"));// will wait for the prompt result and write something like Microsoft Windows bla bla bla ...

此外,如果您想知道这是否真的在做某事,请尝试:

  system("echo SOME TEXT > C:\SOME_FILE.txt"); // tHIS WILL Create a text file in the C disk of windows (but we dont add > NUL because there is already an output path !)

请尝试使用它,阅读更多内容:

more info about