Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
This django-windows default is terrible for lots of reasons (like the fact that many laptops and even the built-in virtual keyboard don't have a break key). I wish I could find a work-around. –  Mortoc Aug 30 '13 at 18:22
    
Python often lies about what to do to exit/stop a process. But you can try other CTRL-<options>. For example: CTRL-C/Z/D –  user1147688 Feb 26 at 22:12
add comment

2 Answers

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.

share|improve this answer
add comment

You may try it in this way:

  1. Your powershell launches your python program in a job

  2. Trap Ctrl-C by the powershell main process

  3. When Ctrl-C is trapped, stop the job launched in step 1

Then, you can keep your powershell script continuing

  1. To launch a background job, use start-job

    $killMe = start-job -scriptblock {&python manage.py}

  2. 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
    }
  }
}
:
  1. 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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.