How to kill process that run on specific port in Windows,Linux and MacOS - C# (.Net Core)
NickName:Hiren Patel Ask DateTime:2018-07-30T13:06:26

How to kill process that run on specific port in Windows,Linux and MacOS - C# (.Net Core)

I need to kill process that uses port 8001 in Windows,Ubuntu and MacOS before starting my application via C# Code,because my application is need to use 8001 on all platform.

So Please Provide me Solution for this problem.

Copyright Notice:Content Author:「Hiren Patel」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/51587273/how-to-kill-process-that-run-on-specific-port-in-windows-linux-and-macos-c-sha

Answers
Duveral 2019-07-31T11:55:31

On macOS and Linux\n\nThis way you dont have to know the PID ;)\n\nsudo kill -9 $(netstat -vanp tcp | grep 8001 | grep -v grep | awk '{print $9}')\n",


Ramanathan Ganesan 2018-08-04T16:39:04

Here is the code sample in C#\nusing System; <br>\nusing System.Diagnostics; <br>\nusing System.Collections.Generic; <br>\nusing System.Text.RegularExpressions; <br>\nnamespace Solution\n{\n public enum Platform\n {\n UNIX, WIN\n }\n\n public class ProcessUtils\n {\n\n public const string UNIX_PID_REGX = @"\\w+\\s+(\\d+).*";\n public const string WIND_PID_REGX = @".*\\s+(\\d+)";\n\n public static void Main(string[] args) {\n Console.WriteLine("Hello World!");\n\n if (args != null && args.Length == 1) {\n findAndKillProcessRuningOn(port: args[0]);\n } else {\n Console.WriteLine("Illegal port option");\n }\n }\n\n public static void findAndKillProcessRuningOn(string port) {\n List<string> pidList = new List<string>();\n List<string> list = new List<string>();\n switch (getOSName())\n {\n case Platform.UNIX:\n list = findUnixProcess();\n list = filterProcessListBy(processList: list, filter: ":" + port);\n\n foreach(string pidString in list) {\n string pid = getPidFrom(pidString: pidString, pattern: UNIX_PID_REGX);\n\n if(!String.IsNullOrEmpty(pid)) {\n pidList.Add(pid);\n }\n }\n break;\n\n case Platform.WIN:\n list = findWindowsProcess();\n list = filterProcessListBy(processList: list, filter: ":" + port);\n\n foreach (string pidString in list)\n {\n string pid = getPidFrom(pidString: pidString, pattern: WIND_PID_REGX);\n\n if (!String.IsNullOrEmpty(pid))\n {\n pidList.Add(pid);\n }\n }\n break;\n default:\n Console.WriteLine("No match found");\n break;\n }\n\n foreach(string pid in pidList) {\n killProcesBy(pidString: pid);\n }\n }\n\n public static Platform getOSName() {\n string os = System.Environment.OSVersion.VersionString;\n Console.WriteLine("OS = {0}", os);\n\n if (os != null && os.ToLower().Contains("unix")) {\n Console.WriteLine("UNXI machine");\n return Platform.UNIX;\n } else {\n Console.WriteLine("Windows machine");\n return Platform.WIN;\n }\n }\n\n public static void killProcesBy(string pidString) {\n int pid = -1;\n if(pidString != null && int.TryParse(s: pidString, result: out pid)){\n Process p = Process.GetProcessById(pid);\n p.Kill();\n Console.WriteLine("Killed pid =" + pidString);\n } else {\n Console.WriteLine("Process not found for pid =" + pidString);\n }\n\n }\n\n public static List<String> findUnixProcess() {\n ProcessStartInfo processStart = new ProcessStartInfo();\n processStart.FileName = "bash";\n processStart.Arguments = "-c lsof -i";\n\n processStart.RedirectStandardOutput = true;\n processStart.UseShellExecute = false;\n processStart.CreateNoWindow = true;\n\n Process process = new Process();\n process.StartInfo = processStart;\n process.Start();\n\n String outstr = process.StandardOutput.ReadToEnd();\n\n return splitByLineBreak(outstr);\n }\n\n public static List<String> findWindowsProcess()\n {\n ProcessStartInfo processStart = new ProcessStartInfo();\n processStart.FileName = "netstat.exe";\n processStart.Arguments = "-aon";\n\n processStart.RedirectStandardOutput = true;\n processStart.UseShellExecute = false;\n processStart.CreateNoWindow = true;\n\n Process process = new Process();\n process.StartInfo = processStart;\n process.Start();\n\n String outstr = process.StandardOutput.ReadToEnd();\n\n return splitByLineBreak(outstr);\n }\n\n public static List<string> splitByLineBreak(string processLines)\n {\n List<string> processList = new List<string>();\n\n if (processLines != null)\n {\n string[] list = processLines.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);\n processList.AddRange(collection: list);\n }\n\n return processList;\n }\n\n public static List<String> filterProcessListBy(List<String> processList, \n String filter) {\n\n if(processList == null) {\n return new List<string>();\n }\n\n if(filter == null) {\n return processList;\n }\n\n return processList.FindAll(i => i != null && i.ToLower().Contains(filter.ToLower()));\n }\n\n public static String getPidFrom(String pidString, String pattern) {\n MatchCollection matches = Regex.Matches(pidString, pattern);\n\n if(matches != null && matches.Count > 0) {\n return matches[0].Groups[1].Value;\n }\n\n return "";\n }\n }\n}\n",


Ramanathan Ganesan 2018-07-30T07:01:35

Windows\n\n\nFind process ID\n\n\ncommand netstat -a -b\nResource monitor from (Start>>All Programs>>Accessories>>System Tools>>Resource Monitor)\n\nkill the process using the below command taskkill /PID pid\n\n\nUNIX and MAC\n\n\nFind process ID using lsof -i | grep <process id>\nkill the process using the command kill -9 <process id>\n",


More about “How to kill process that run on specific port in Windows,Linux and MacOS - C# (.Net Core)” related questions

How to kill process that run on specific port in Windows,Linux and MacOS - C# (.Net Core)

I need to kill process that uses port 8001 in Windows,Ubuntu and MacOS before starting my application via C# Code,because my application is need to use 8001 on all platform. So Please Provide me

Show Detail

Kill a process using a port (Windows/C)

How can I kill (or just find the pid. The killing is easy) of a process that is listening on a certain port from c/c++ on Windows?

Show Detail

How to kill process on port on Windows when the port cannot be opened?

I'm on a Windows machine. Typically, when I need to kill a process on a port I do the following: netstat -ano | findstr : &lt;PORT&gt; After that command, the Process Identifier (PID) should appear...

Show Detail

How to kill a process on a port on ubuntu

I am trying to kill a process in the command line for a specific port in ubuntu. If I run this command I get the port: sudo lsof -t -i:9001 so...now I want to run: sudo kill 'sudo lsof -t -i:90...

Show Detail

Kill process that is taking specific port

Is is possible to programatically select process that is taking a specific port (:3000 for example) and kill it ? I do that by hand now by using netstat -tp and then I would check pid of a proce...

Show Detail

One command to kill a process using a specific port

For a project, I constantly need to find the process using a specific port and kill that process. I do this by: lsof -i :portnumber #read the pid kill -9 pid Since I do this a lot, I'm a bit bore...

Show Detail

How to kill a process running on particular port in Linux?

I tried to close the tomcat using ./shutdown.sh from tomcat /bin directory. But found that the server was not closed properly. And thus I was unable to restartMy tomcat is running on port 8080. I ...

Show Detail

How do I kill a specific process running in the background from the command prompt?

Assuming this is a Windows 7 machine - and we're talking about batch scripts on the Windows command line. Imagine I want to start and stop two different processes running in the background, and run

Show Detail

How to kill a windows process by Id in C?

I searched many websites for this issue but I couldn't find a good answer. I'm getting a pid from a text file and then trying to kill that process with Kill() function but when I try to compile I see

Show Detail

Linux/MacOs - Know which process filter UDP 443 packets

Is there a way, on Linux / MacOs to find which process is filtering my UDP packets on a specific port ? Here some details and why I'm asking: On my MacOs ( Mojave 10.14) , if I try to send a UDP ...

Show Detail