Search code examples
rubyprocesspid

How can I check from Ruby whether a process with a certain pid is running?


If there is more than one way, please list them. I only know of one, but I'm wondering if there is a cleaner, in-Ruby way.


Solution

  • If it's a process you expect to "own" (e.g. you're using this to validate a pid for a process you control), you can just send sig 0 to it.

    >> Process.kill 0, 370
    => 1
    >> Process.kill 0, 2
    Errno::ESRCH: No such process
        from (irb):5:in `kill'
        from (irb):5
    >>