I need to run Python programs that continue running even after I close the PowerShell terminal window. In Linux bash, I use:
nohup python my_program.py &
This runs the program in the background and it survives terminal closure.
What is the PowerShell equivalent of nohup command &
?
python my_program.py
directly - process dies when terminal closesStart-Process
without parameters - still terminates with terminalUse the Start-Process
cmdlet via conhost.exe
, which on Windows launches an independent process in a new console window (by default), use the -WindowStyle
parameter to control the visibility / state of that window; e.g., to launch in a hidden window:
Start-Process -WindowStyle Hidden conhost.exe 'python my_program.py'
Note:
Launching via conhost.exe
is necessary on machines that use Windows Terminal as the default console (terminal application), which applies to recent versions of Windows by default.
Without conhost.exe
, the newly launched process would by default open in a new tab of Windows Terminal, and therefore still subject to termination on closing Windows Terminal.
Do not use -NoNewWindow
, as that would close the python
process along with the current console window.
The above doesn't work in the context of PowerShell remoting; see the bottom section of this answer for details and a workaround.
On Unix-like platforms, where Start-Process
doesn't support creating independent new terminal windows, you must additionally use nohup
- see this answer.