2

I run PHP CLI on Windows 10. I have installed Windows Terminal via winget. For several reasons, I have long tried to switch from using cmd.exe to using either PowerShell or Windows Terminal for my PHP CLI scripts.

But, after spending a full day on experimenting with this, I have determined the following:

  1. Launching 100 PHP CLI scripts with cmd.exe barely is noticeable in CPU and RAM usage. I do this all the time. (Automatically, in the background, all launching minimized and grouping to the same Taskbar icon, performing all kinds of tasks concurrently.)

  2. Launching 100 PHP CLI scripts with PowerShell or Windows Terminal (yes, even with new tabs instead of new separate instances!) causes my powerful computer to become unusable. All CPU and tons of RAM is used, and all kinds of error messages start popping up and the Taskbar icon becomes impossible to click to show the windows; everything "glitches out".

  3. If I execute a command such as "php test.php" directly, without any terminal attached, it opens a PHP-branded window of some sort, but appears to use a cmd.exe "behind the scenes", so this is just the same as explicitly using cmd.exe.

My conclusion is that it's practically impossible to use anything other than cmd.exe for PHP CLI. I have searched, asked and experimented a lot related to this.

The main reason I needed to switch from cmd.exe in the first place is that the window changes styles whenever a PowerShell command is called from a PHP CLI script which was launched as/with cmd.exe, which does not happen if I use PowerShell or Windows Terminal.

But since both PowerShell and Windows Terminal are so extremely resource-hungry, I just cannot use them.

Is there some secret way to launch "minimal" versions of them or something? It doesn't seem like I could even buy a machine powerful enough to power my (relatively) few CLI instances.

I did not expect this to be an issue, and I don't understand how it can be the case no matter how "advanced" these terminals may be.

4
  • Please add some screenshots of the error messages popups that you get.
    – harrymc
    Commented Apr 8, 2023 at 10:24
  • 1
    Look at Powershell /? options from command line. There are switches to pass to powershell.exe such as -NoProfile or -NonInteractive that may help make the powershell.exe footprint smaller. I would never process 100 processes at the same time concurrently and ensure whatever scripted processes I am launching is optimized for the system which is it running, throttle or queue the processes for x thread, etc. I don't see why you need 100 terminal sessions active anyway and you should log your processes and look at the logs for whatever you need the terminal open for output wise, etc. Commented Apr 8, 2023 at 14:10
  • So change your 'terminal sessions open per PHP CLI execution' and figure out whatever ultimate goal you have (reason you need 100 instances opened and minimized, tabs, etc.) and figure out a more optimized way to handle the task per whatever "requirement" constraints you have. PowerShell is not cmd so if you must use PowerShell, this is a downside, time to change strategy. Take that back to the powers that tell you any new requirements and let them know this is a downside to the technology and then brainstorm and test to optimize your processes on the system you run them with the tool you use. Commented Apr 8, 2023 at 14:13
  • We cannot tell you why different programs behave differently, we aren't the devs. I recommend you edit your question including the title, to remove this unanswerable part and focus on an specific question we CAN answer. Commented Apr 9, 2023 at 20:53

2 Answers 2

2

If I execute a command such as "php test.php" directly, without any terminal attached, it opens a PHP-branded window of some sort, but appears to use a cmd.exe "behind the scenes", so this is just the same as explicitly using cmd.exe.

It does not. Rather, both php.exe and cmd.exe (and PowerShell.exe, for that matter) use the same kind of automatic console window that is nowadays known as "Conhost" as of Vista, previously part of CSRSS in XP.

Conhost is the built-in terminal/console that is automatically opened for all such apps that have the 'console' flag set in their .exe files (though until Windows 8.1 it did not have any of the Unix-style terminal control capabilities, hence the term "console" rather than "terminal").

(It's actually Conhost that also "remembers" the console window appearances per-executable and/or per-shortcut, causing e.g. the PowerShell.exe console to have a blue background by default – though I suspect PowerShell may also deliberately tweak its console font on startup as well.)

Cmd.exe and PowerShell on their own have no visual appearance (not counting the old PowerShell ISE, that is) – they both always run either in Conhost or in Windows Terminal, with the OS automatically opening Conhost if needed. (As of Windows 11, the OS can automatically open Windows Terminal instead.)

Launching 100 PHP CLI scripts with PowerShell or Windows Terminal (yes, even with new tabs instead of new separate instances!) causes my powerful computer to become unusable. All CPU and tons of RAM is used, and all kinds of error messages start popping up and the Taskbar icon becomes impossible to click to show the windows; everything "glitches out".

Windows Terminal uses GPU rendering (as does the Windows 10 Start Menu UI), so I'm guessing this probably indicates inefficiencies in its architecture, causing them to compete for resources. Try to enable the alternative "Atlas" rendering engine through its profile settings.

Is there some secret way to launch "minimal" versions of them or something? It doesn't seem like I could even buy a machine powerful enough to power my (relatively) few CLI instances.

You're already doing that. The Conhost console window you get when running either php.exe or cmd.exe is as minimal as it gets.

8
  • If they all use the same console window, why is powershell so much slower? Why does the OP experience different results? I can attest to cmd.exe being faster than powershell.exe for running multiple instances of another program.
    – GChuf
    Commented Apr 7, 2023 at 8:35
  • Depends on which part of it is actually slower? PowerShell itself is slow to start up, but I'm not sure when the "other program" comes in, in your case. Commented Apr 7, 2023 at 8:39
  • "the other program" as you put it is just a binary that does something. The whole problem is that calling that binary through windows terminal or powershell is slower and hogs more resources than cmd. Hence the question how to make that faster.
    – GChuf
    Commented Apr 7, 2023 at 8:45
  • Again, which part of it? Does the binary itself occupy more memory or take more CPU to perform the same task when it's run from within PowerShell, or does PowerShell just take longer to spawn the binary initially? Is it the binary, or the PowerShell process, or the Conhost/WindowsTerminal process that's using those resources? Commented Apr 7, 2023 at 9:00
  • I understand your question now. In my own experience, the binary performs the same. It's the (powershell/windows terminal)conhost process that takes a lot of memory, and a lot of time to start up.
    – GChuf
    Commented Apr 7, 2023 at 9:07
1

FWIW, I think you've got some confusion between what a terminal and a shell is. The Windows Terminal is a terminal emulator, which can run any shell, like CMD or PowerShell. "Conhost" (the vintage console window) was similarly a "terminal". @user1686 said as much above.

The Windows Terminal sets PowerShell as the default shell, but you can always change that to cmd.exe in the settings.

Can you file a minimal repro over on https://github.com/microsoft/terminal? The Terminal should be a little more resource intensive than conhost, sure. But I wouldn't expect the scenario you're describing to be as bad as you mention with the Windows Terminal set as the default terminal. It kinda sounds like whatever script is running is intentionally opening a pile of console windows, rather than reusing the one it was launched from. Or even more subtly - it's not passing the right flags to CreateProcess to create the console window hidden (which would keep Terminal out of the equation).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .