开发者

How to check wether a process with a given PID is running?

开发者 https://www.devze.com 2023-02-03 05:28 出处:网络
I have a program written in C#, running on开发者_如何学编程 Linux using Mono. The program writes its PID to a file. Now I want to check wether a process with this PID is running on start up. Is there

I have a program written in C#, running on开发者_如何学编程 Linux using Mono. The program writes its PID to a file. Now I want to check wether a process with this PID is running on start up. Is there a way using Mono? If not, how can I accomplish this using standard Linux functions/tools? Or do I have to check /proc/PID/cmdline?


Standard functionality would be to run kill -0 PID to see if the process is running and check the exit code, 0 function is running.


You could also check that the directory /proc/$PID exists (no need to execute system commands). Should work on Linux, dunno if on other flavors of Unix.

And there is also: System.Diagnostics.Process.GetProcessById(int id) which should throw an exception if the process with given id is not running. Dunno if it works on Mono.

0

精彩评论

暂无评论...
验证码 换一张
取 消