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.
精彩评论