START /B with php/node/python script doesn't w

2019-05-07 12:25发布

问题:

I start a shell and I run the following command:

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");

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.

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.

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.

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.

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.

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

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...

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?

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.

Here's a gif animation of exactly what happens:

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

回答1:

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

start "" /b php test.php <NUL

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



回答2:

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)

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:

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:

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

I tested with

test.php

<?php
echo "Hello";
?>

And

start "myTitle" /b php test.php


回答4:

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.

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.