30

I am doing this basic tutorial: http://www.asp.net/get-started It suggests doing some changes, then running dotnet run again. However, Ctrl+C isn't working and the Package Manager Console seems frozen. When I exit VS and/or restart it, I do my changes and dotnet run again. When I am doing this, I am getting an error (not the same every time), because the server is already running. The question is simple as 1-2-3: How do I manually stop the kestrel server? (I am running Windows 10).

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Petros Apotsos
  • 615
  • 2
  • 6
  • 13
  • If you ran from the command line: Ctrl + C – Bruno Garcia Aug 11 '16 at 21:34
  • @BrunoGarcia I forgot to mention that doesn't work! Also, when I quit VS and/or restart it, the process still runs (page refreshes on Chrome)! Where do I find the relevant process and how do I kill it? I will Update the Q with extra info...Thanx! – Petros Apotsos Aug 11 '16 at 21:56
  • Related [issue](https://stackoverflow.com/questions/43306332/stop-a-running-dotnet-core-website-running-on-kestrel). This is currently an issue with the cli [terminating `dotnet run` doesn't terminate child #7426](https://github.com/dotnet/cli/issues/7426) – stormwild Oct 01 '17 at 04:49

9 Answers9

36

On OSX Terminal:

sudo lsof -iTCP -sTCP:LISTEN -P | grep :5000

enter image description here

sudo kill -9 1872
Javier Flores
  • 597
  • 4
  • 7
30

I ran this on powershell to kill all instances of the kestral server

Get-Process -Name *dotnet* | Stop-Process
DarkFm
  • 430
  • 6
  • 7
9

For windows users:

Run netstat -ano -p TCP | find /I "listening" | find /I "port number" then run taskkill /F /PID process ID on Command Prompt.

Here is an example

C:\Users\Kiran>netstat -ano -p TCP | find /I "listening" | find /I "2492"
TCP    127.0.0.1:2492         0.0.0.0:0              LISTENING       820

C:\Users\Kiran>taskkill /F /PID 820
SUCCESS: The process with PID 820 has been terminated.

Here netstat -ano -p TCP | find /I "listening" | find /I "2492" finds the process running TCP protocol listening on port 2492. And taskkill /F /PID 820 command to forcefully terminate the process with id 820

Albert Einstein
  • 7,472
  • 8
  • 36
  • 71
8

If anything else fails you can simply close it from the task manager. The name of the process would be dotnet. If you have Visual Studio open (in my case at least), there may be at least one other dotnet process running. Could not distinguish between them using only the task manager...

PS: When launching Kestrel with dotnet run from the PMC, in the console you get this:

Now listening on: http://localhost:5000 Application started. Press Ctrl+C to shut down.

But it didn't work to stop it with Ctrl+C

Albert Einstein
  • 7,472
  • 8
  • 36
  • 71
popliviustefan
  • 121
  • 1
  • 7
  • I don't see 'dotnet' in my list of tasks even though supposedly Kestrel is holding onto a site running in docker (even though I've fully stopped and removed the docker container) – emirhosseini Feb 22 '18 at 05:22
5

The line below works for me.

killall -9 dotnet

Melf
  • 129
  • 2
  • 4
4

Stumbled upon it today. The answer above looks cool but netstat is slow. A simpler way to kill kestrel would be to use the PowerShell commandlet Get-NetTCPConnection.

To kill kestrel listening on Port 5000, the command would be

$pId = (Get-NetTCPConnection -LocalPort 5000).OwningProcess[0]
kill -Id $pId
Hamid Shahid
  • 4,486
  • 3
  • 32
  • 41
1

Save the following in a .cmd file and click on the file:

Taskkill.exe /F /IM dotnet.exe /T
RickAndMSFT
  • 20,912
  • 8
  • 60
  • 78
0

Ctrl + Break also does the trick.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
0

For macOS 12.6 (Monterey) with .NET Core 6 and 7, this command worked for me.

pgrep -f dotnet | xargs kill -9

PieterT2000
  • 304
  • 2
  • 12