使用php / node / python脚本的START / B无法正常工作

时间:2022-05-25 20:55:00

I start a shell and I run the following command:

我启动一个shell,然后运行以下命令:

START "" /B php test.php>nul 2>&1

For the purposes of this post, test.php consists of 1 line: file_put_contents("test", "test");

出于本文的目的,test.php由1行组成:file_put_contents(“test”,“test”);

You'd expect it to instantly create a file called test, however it doesn't. Instead, when I enter the command and hit enter, nothing happens. However, in the same command window, if I hit the enter key once more, the script will run.

你希望它立即创建一个名为test的文件,但事实并非如此。相反,当我输入命令并按Enter键时,没有任何反应。但是,在同一个命令窗口中,如果再次按Enter键,脚本将运行。

I've tried the equivalent command with node, python2 and python3. They all do the same thing, although python3 oiddly enough requires 2 enter key presses before the script is run.

我已尝试使用node,python2和python3的等效命令。它们都做同样的事情,虽然python3足够需要在运行脚本之前按两次回车键。

This appears to be a quirk when running START /B with script interpreters. If I ran START /B with an .exe or .bat then it runs as expected.

使用脚本解释器运行START / B时,这似乎是一个怪癖。如果我用.exe或.bat运行START / B,那么它会按预期运行。

Now, obviously I don't need to run a background process that creates a file named 'test'. What I need it to do is to start a daemon process written in php and a daemon process written in nodejs, one after the other in the same terminal.

现在,显然我不需要运行创建名为'test'的文件的后台进程。我需要它做的是启动一个用php编写的守护进程和一个用nodejs编写的守护进程,一个接一个地在同一个终端中。

I've also noticed if I write these commands to a batch file and run them by double clicking the batch file instead of calling it from the shell, then it all runs as expected.

我还注意到,如果我将这些命令写入批处理文件并通过双击批处理文件而不是从shell调用它来运行它们,那么它都按预期运行。

Please help. I've been wrestling with this problem for 2 days now.

请帮忙。我已经和这个问题摔跤了2天了。

EDIT:

I've just found that this problem is unique to my computer, running on Windows 10. I tried on Windows 7 and Windows XP and they did not require extra return inputs from me to run the script...

我刚刚发现这个问题对于我的计算机来说是独一无二的,在Windows 10上运行。我试过Windows 7和Windows XP,他们不需要额外的返回输入来运行脚本......

I've just tried booting up in safe mode and clearing my path variables. Still it persists. I wonder if this is a bug/feature of Windows 10? How the hell do I diagnose and fix this?

我刚尝试在安全模式下启动并清除路径变量。它仍然存在。我想知道这是否是Windows 10的错误/功能?我该怎么诊断和解决这个问题?

EDIT 2:

Just tested the same script on a Windows 8 machine, which unexpectedly does exactly what my Windows 10 machine does. SO it looks like only Windows 8 and Windows 10 have this problem... And using legacy mode makes no difference. What the FFFFFFFFFnnngggghhhrrr... I can't find anyone reporting this issue anywhere.

刚刚在Windows 8计算机上测试了相同的脚本,这意外地完成了我的Windows 10计算机所做的事情。所以看起来只有Windows 8和Windows 10才有这个问题......并且使用传统模式没有任何区别。什么是FFFFFFFFFnnngggghhhrrr ...我找不到任何人在任何地方报告这个问题。

Here's a gif animation of exactly what happens:

这是一个gif动画,确切地说会发生什么:

使用php / node / python脚本的START / B无法正常工作

Proof that the script is only run on the second return key press.

证明脚本仅在第二个返回键上运行。

4 个解决方案

#1


1  

You could try <NUL to send an additional Enter to the command.

您可以尝试 向命令发送额外的enter。

start "" /b php test.php <NUL

Re: why this happens, sorry, I couldn't answer that part. Bug? Security? Undocumented feature? *shrug*

Re:为什么会发生这种情况,抱歉,我无法回答这一部分。错误?安全?没有文档的功能? *耸肩*

#2


0  

start /b starts another process, which shares Input and Output with the original Windows. (to verify do start /b "" prompt $G and press Return repeatedly)

start / b启动另一个进程,该进程与原始Windows共享Input和Output。 (验证是否开始/ b“”提示$ G并反复按Return键)

Better start the secondary process in another Windows (no /b - use /min instead to run it minimized). You also should give it an Exit, else it keeps open:

最好在另一个Windows中启动辅助进程(没有/ b - 使用/ min而不是最小化运行它)。你也应该给它一个退出,否则它保持打开:

start /min "" echo hello^>hello.txt^&Exit

Note: if you don't escape special chars like >, &, |,..., they will be processed by "start" - in other words, in your original context, not in the newly started process.

注意:如果你没有逃避像>,&,|,...这样的特殊字符,它们将由“开始”处理 - 换句话说,在原始上下文中,而不是在新启动的过程中。

#3


0  

I can't see any problem here.
Perhaps you corrupted your cmd.exe with some of your tests.
For the tests I opened a new cmd window

我在这里看不到任何问题。也许您已经使用某些测试损坏了cmd.exe。对于测试,我打开了一个新的cmd窗口

I tested with

我测试过

test.php

<?php
echo "Hello";
?>

And

start "myTitle" /b php test.php

#4


0  

I had very similar problem and stumbled on this SO question while trying to find an answer.

我有类似的问题,在试图寻找答案时偶然发现了这个问题。

In my case I was trying to run a windows command from PHP using 'popen'.
I had recently upgraded Wampserver as well as Windows 10 from 8.
My code worked prior to the upgrade and subsequently was timing out.
I found the same issue on command line as well - having to input newlines to receive output.

在我的情况下,我试图使用'popen'从PHP运行Windows命令。我最近从8升级了Wampserver和Windows 10.我的代码在升级之前工作,随后超时。我在命令行上也发现了同样的问题 - 必须输入换行符才能接收输出。

My solution was two fold:
1. On the command line I found that adding the '/wait' switch fixed that.
2. The shell used by PHP had a different PATH to that of the cmd shell. My path to php.exe was defined twice, there was an old entry for my old version of Wampserver. So I removed it. Note I had to restart PC for the path to update correctly.

我的解决方案有两个:1。在命令行上,我发现添加'/ wait'开关修复了这个问题。 2. PHP使用的shell具有与cmd shell不同的PATH。我的php.exe路径定义了两次,我旧版本的Wampserver有一个旧条目。所以我删除了它。注意我必须重新启动PC才能正确更新路径。

#1


1  

You could try <NUL to send an additional Enter to the command.

您可以尝试 向命令发送额外的enter。

start "" /b php test.php <NUL

Re: why this happens, sorry, I couldn't answer that part. Bug? Security? Undocumented feature? *shrug*

Re:为什么会发生这种情况,抱歉,我无法回答这一部分。错误?安全?没有文档的功能? *耸肩*

#2


0  

start /b starts another process, which shares Input and Output with the original Windows. (to verify do start /b "" prompt $G and press Return repeatedly)

start / b启动另一个进程,该进程与原始Windows共享Input和Output。 (验证是否开始/ b“”提示$ G并反复按Return键)

Better start the secondary process in another Windows (no /b - use /min instead to run it minimized). You also should give it an Exit, else it keeps open:

最好在另一个Windows中启动辅助进程(没有/ b - 使用/ min而不是最小化运行它)。你也应该给它一个退出,否则它保持打开:

start /min "" echo hello^>hello.txt^&Exit

Note: if you don't escape special chars like >, &, |,..., they will be processed by "start" - in other words, in your original context, not in the newly started process.

注意:如果你没有逃避像>,&,|,...这样的特殊字符,它们将由“开始”处理 - 换句话说,在原始上下文中,而不是在新启动的过程中。

#3


0  

I can't see any problem here.
Perhaps you corrupted your cmd.exe with some of your tests.
For the tests I opened a new cmd window

我在这里看不到任何问题。也许您已经使用某些测试损坏了cmd.exe。对于测试,我打开了一个新的cmd窗口

I tested with

我测试过

test.php

<?php
echo "Hello";
?>

And

start "myTitle" /b php test.php

#4


0  

I had very similar problem and stumbled on this SO question while trying to find an answer.

我有类似的问题,在试图寻找答案时偶然发现了这个问题。

In my case I was trying to run a windows command from PHP using 'popen'.
I had recently upgraded Wampserver as well as Windows 10 from 8.
My code worked prior to the upgrade and subsequently was timing out.
I found the same issue on command line as well - having to input newlines to receive output.

在我的情况下,我试图使用'popen'从PHP运行Windows命令。我最近从8升级了Wampserver和Windows 10.我的代码在升级之前工作,随后超时。我在命令行上也发现了同样的问题 - 必须输入换行符才能接收输出。

My solution was two fold:
1. On the command line I found that adding the '/wait' switch fixed that.
2. The shell used by PHP had a different PATH to that of the cmd shell. My path to php.exe was defined twice, there was an old entry for my old version of Wampserver. So I removed it. Note I had to restart PC for the path to update correctly.

我的解决方案有两个:1。在命令行上,我发现添加'/ wait'开关修复了这个问题。 2. PHP使用的shell具有与cmd shell不同的PATH。我的php.exe路径定义了两次,我旧版本的Wampserver有一个旧条目。所以我删除了它。注意我必须重新启动PC才能正确更新路径。