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

前端 未结 9 1915
眼角桃花
眼角桃花 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:01

    I've dealt with this problem before and yesterday I compiled it into the "process_exists" gem.

    It sends the null signal (0) to the process with the given pid to check if it exists. It works even if the current user does not have permissions to send the signal to the receiving process.

    Usage:

    require 'process_exists'
    
    pid = 12
    pid_exists = Process.exists?(pid)
    

提交回复
热议问题