New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exiting pwsh kills the process that was started with Start-Process #21149
Comments
@jborean93 has more in-depth knowledge in this area, but I think this comes down to platform differences:
|
Going back in history, how Windows ran an EXE depended on the SUBSYSTEM of the EXE. If it was compiled with /SUBSYSTEM:CONSOLE it was considered a command line app and the command processor would wait for it to exit. If it did not have that SUBSYSTEM it was assumed to be a GUI app, and when started it was effectively detached. Start-Process does indeed say
Unfortunately |
Thanks, @rhubarb-geek-nz (the Re Windows console-subsystem applications: What you're describing applies to direct invocation, but |
This is not a good workaround with What about windows? The problem also exists. |
Is this true? In UNIX you may have a process group sharing a common controlling terminal. When the controlling terminal is disconnected it sends SIGHUP to all processes with that controlling terminal. The default behaviour for SIGHUP is to terminate the process, the program "nohup" breaks this connection by preventing the SIGHUP signal. Nohup does not actually remove the process from the controlling terminal |
Thanks for the clarification re process groups, @rhubarb-geek-nz - I've updated the wording in my earlier comment (meant to be a summary of sorts) and added a link to yours. @237dmitry, there is no problem on Windows that I'm aware of, except the constraints mentioned earlier: use of |
Similarly, @237dmitry, it may be worth creating a separate feature request for building the functionality of |
If you are not looking for portable code then simply use
shows sleep is still running after powershell exit |
One downside of Windows itself is the assumption that everything needs Windows and that command line programs need console windows. Often on a Windows update or an installation of an MSI you get console windows flashing up for fractions of a second, it is rather unfortunate. |
This works like Start-Process. I'm using a script to launch a new alacritty terminal instance with different profiles (bash, pwsh, mc, htop, musikcube). There are variables and Start-Process is a more convenient way to do this. Using nohup has disadvantages, since it outputs messages to the console, and I have not yet been able to redirect them to /dev/null. PS. I want to switch from Tilix to Alacritty. |
@237dmitry, what happens on Windows is this:
The workaround is to launch via a hidden aux. console window; e.g.: Start-Process -WindowStyle hidden cmd '/c alacrity --config %APPDATA%\alacritty\pwsh.toml' Pragmatically speaking, you could wrap the above in an As for what should be done:
|
Thank you. I'll definitely try this. |
@237dmitry, as for a Unix workaround: You can silence sh -c '{ nohup sleep 30 & } 2>/dev/null 1>&2' |
On Linux nohup only writes to nohup.out if the stdout is directed at a terminal
|
This comment was marked as resolved.
This comment was marked as resolved.
I did not tried this yet in pwsh scripts
|
From PowerShell, If you don't mind Start-Process nohup 'sleep 30' -RedirectStandardError /dev/null The following should work and would prevent creation of Start-Process nohup 'sleep 30' -RedirectStandardOut /dev/null -RedirectStandardError /dev/null Note: On macOS (as opposed to Linux), # macOS only: prevents both console output and creation of file nohup.out
Start-Process nohup 'sleep 30' -RedirectStandardOut /dev/null |
Wau!!! Thank you very much!
Works fine! UPD. On Windows with Start-Process cmd -ArgumentList "/c alacritty --title $title --config-file $config" -WindowStyle Hidden |
Re console-subsystem applications currently invariably creating a visible console window unless attached to an existing console - which notably manifests as an inability to invoke PowerShell itself hidden, see #3028 - there's a pending PR for a Console Application Policy (now 3+ years old, most recently amended in Dec 2023), proposing a way for applications to control their console-related behavior via their manifest: |
Java solved the problem by having "java.exe" and "javaw.exe", so the first one was a console app, the second was a GUI app. However you may still want stdin/stdout/stderr attached. |
@rhubarb-geek-nz, an express design goal of the linked PR is to avoid the need for separate executables going forward (the |
If you use CreateProcess() with DETACHED_PROCESS it is the responsibility of the new process to call AllocConsole() if it wants a console window, rather than the parent making the assumption and using CREATE_NEW_CONSOLE. |
|
That seems very convoluted and not in the right place at all, creating console windows should have nothing to do with the kernel. In the UNIX world controlling terminals are either system configured for serial ports or pseudo-terminals are created on demand at login by either rlogind, telnetd or sshd, or created when a new terminal window is opened (eg xterm, dtterm, gnome-terminal etc). |
I'm not disagreeing; Windows has a lot of historical baggage, stemming from questionable initial design choices coupled with a commitment to backward compatibility - arguably, PowerShell itself has the same affliction (and I'm saying this as someone who is a fan of the fundamentals of PowerShell). |
This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes. |
📣 Hey @237dmitry, how did we do? We would love to hear your feedback with the link below! 🗣️ 🔗 https://aka.ms/PSRepoFeedback |
Prerequisites
Steps to reproduce
This behavior occurs on Linux and Windows.
On Windows it is not always possible to reproduce, depending on the application.
No problem:
Hangs console window (conhost), killing it (Ctrl-C) kills alacritty too:
Expected behavior
Process started with Start-Process should not depend on pwsh
Actual behavior
Process started with Start-Process terminates when pwsh closes
Error details
No errors
Environment data
Visuals
No response
The text was updated successfully, but these errors were encountered: