Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Killing a python script in C#

I'm wondering if it's possible to kill a python script using C#.

In the small application I'm currently developing, a python application launches to localhost:portnumber. The portnumber of the application is always the same.

Is it possible, when the application is already running (I'm checking that by getting a list of ports currently in use), to kill it using some sort of command?

I've already found out that, in case it is not running, I can start the application using Process.Start();

like image 588
DescX Avatar asked Jun 06 '26 10:06

DescX


1 Answers

You can use Process.Kill() to kill a specific process...
To find out which process to kill you can use GetProcesses() and itterate through them...

For example, here is how you can kill the calculator (calc.exe):

 foreach (Process process in Process.GetProcesses().Where(p => 
                                                         p.ProcessName == "calc"))
 {
     process.Kill();
 }

This will find all processes named "calc" and kill them.

In your case, If you already have a Process object (because you called Process.Start() ) you can specifically kill that one using the Kill() method.

like image 108
Blachshma Avatar answered Jun 09 '26 00:06

Blachshma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!