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

前端 未结 9 1895
眼角桃花
眼角桃花 2021-01-30 16:18

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.

9条回答
  •  盖世英雄少女心
    2021-01-30 17:00

    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
    >> 
    

提交回复
热议问题