Finding and Killing .NET Processes that you Disconnect from Instead of Stopping while Debugging on Linux

To see how to do this in Windows check this post.

When using Visual Studio Code to debug an application, I sometimes accidentally “Disconnect” the debug session instead of “Stopping” it. The same thing happens to me with Rider. When I “Disconnect”, the process continues running, and I won’t be able to start another debug session until I kill the one that is running.

Finding the process

Compared to Windows, in Linux, it is very easy to find the relevant process ID.

Let’s say the application is called “MyWebAPIApp”, all I need to do is run -

pgrep -afi mywebapiapp

Here are what the options do -

  • -a list PID and full command line
  • -f use full process name to match
  • -i match case insensitively

I will get output like this -

17568 /home/bryan/dotnet/dotnet /home/bryan/dev/MyWebAPIApp/bin/Debug/net7.0/MyWebAPIApp.dll

Then kill that process -

kill 17568

Finding the process via the port

If you want to find the process that is using a known port number, you can use a few different methods. Let’s say the port is 5125.

Here are some of the ways, with their respective output -

# netstat
$ netstat -lnpt | grep 5125
tcp        0      0 127.0.0.1:5125          0.0.0.0:*               LISTEN      22435/dotnet        
tcp6       0      0 ::1:5125                :::*                    LISTEN      22435/dotnet 

# ss
$ ss -lnpt | grep 5125
LISTEN 0      512        127.0.0.1:5125       0.0.0.0:*    users:(("dotnet",pid=22435,fd=326))
LISTEN 0      512            [::1]:5125          [::]:*    users:(("dotnet",pid=22435,fd=327))

# lsof
$ lsof -i :5125
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
dotnet  22435 bryan  326u  IPv4 135254      0t0  TCP localhost:5125 (LISTEN)
dotnet  22435 bryan  327u  IPv6 135258      0t0  TCP ip6-localhost:5125 (LISTEN)

# fuser
$ fuser 5125/tcp
5125/tcp:            22435

comments powered by Disqus

Related