4

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:

enter image description here

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

13
  • 1
    What does it do if you remove >nul 2>&1? Commented Dec 7, 2015 at 3:01
  • It's definitely something to do with the >nul 2>&1. I think the first >nul is not escaped properly Commented Dec 7, 2015 at 3:53
  • Nope, tried that. Try entering it directly into the console yourself and you should experience the same unusual behaviour: START "" /B php test.php>nul 2>&1 won't run on the first return. Commented Dec 7, 2015 at 11:33
  • I've modified my post, looks like it's unique to me, still no closer to fixing it though... Help me please. Commented Dec 7, 2015 at 13:02
  • OK, just realised the problem is not unique to me, but to Windows 8 and 10 OS's. Completely flummoxed... Commented Dec 7, 2015 at 13:23

4 Answers 4

1
+50

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*

Sign up to request clarification or add additional context in comments.

4 Comments

I'm afraid this runs exactly the same as before. Still requires I press return on the following empty command
@hedgehog90 See my edit. We'll trial and error this thing. :)
<NUL WORKED! Thank you! ... buuuuut, I need to find out why this happens.
I just gave you the 50 rep points, I doubt we'll get to the bottom of why without asking Microsoft directly. Thanks for the help :)
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)

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.

2 Comments

Problem is the php script I actually want to run is a socket listener, that extra CMD window will need to stay open. I think I said in my OP that I also need an instance of a node script running as well, so that would make 3 CMD windows in total.
well, then forget the exit. The point is to run the script in it's own window - no "shared input" (I don't have php, but I'm quite sure that's not the problem. It's the "shared input" that works - well - weird)
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

I tested with

test.php

<?php
echo "Hello";
?>

And

start "myTitle" /b php test.php

1 Comment

Which OS are you running? I only have the issue on Windows 8 and 10
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.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.