Keeping PowerShell Open after CTRL-BREAK
I am working with Python Django in Windows Vista Powershell. After I run python manage.py runserver
,
it asks me to CTRL-BREAK
to stop the process. But after I typed that, Powershell stops the process and
closes. Is there a way to stop the process and keep the Powershell open?
Solution 1:
Unfortunately the PowerShell console host uses Ctrl+C
to terminate an ongoing operation and Ctrl+Break
terminates the operation AND closes the host session. It's a most unfortunate choice IMHO but it's there and we get to live with it.
I find the easiest thing to do when I run into situations like this is from the PowerShell session simply execute cmd
from the PowerShell prompt. Now I'm actually bypassing PowerShell temporarily and I'm easily back to my PowerShell session when done.
The other thing you should probably consider is using cmd.exe rather than PowerShell unless there is some feature of PowerShell you're trying to leverage.
Solution 2:
You may try it in this way:
Your powershell launches your python program in a job
Trap Ctrl-C by the powershell main process
When Ctrl-C is trapped, stop the job launched in step 1
Then, you can keep your powershell script continuing
-
To launch a background job, use start-job
$killMe = start-job -scriptblock {&python manage.py}
-
To trap Ctrl-C, tell your console to treat Ctrl-C as an input, check this from MSTN.
[console]::TreatControlCAsInput = $true
Then run an UI loop for checking keys input:
while ($true) {
write-host "Processing..."
if ([console]::KeyAvailable) {
$key = [system.console]::readkey($true)
if (($key.modifiers -band [consolemodifiers]"control") -and
($key.key -eq "C")) {
:
break
}
}
}
:
-
To stop the job when the "Ctrl-C" is hit,
stop-job -job $killMe
However, life will not be easy if you need to interact with your python server, which is running at the background. And you would probably like to display your server's output constantly.