13

我使用调度程序(Rufus 调度程序)每分钟启动一个名为“ar_sendmail”(来自 ARmailer)的进程。

当已经有这样的进程正在运行时,不应启动该进程,以免耗尽内存。

如何检查此进程是否已在运行?下面是什么unless

scheduler = Rufus::Scheduler.start_new

  scheduler.every '1m' do

    unless #[what goes here?]
      fork { exec "ar_sendmail -o" }
      Process.wait
    end

  end

end
4

3 回答 3

24
unless `ps aux | grep ar_sendmai[l]` != ""
于 2011-01-04T13:28:25.210 回答
8
unless `pgrep -f ar_sendmail`.split("\n") != [Process.pid.to_s]
于 2015-04-08T08:48:31.370 回答
1

我认为这看起来更整洁,并且使用了内置的 Ruby 模块。发送 0 杀信号(即不杀):

  # Check if a process is running
  def running?(pid)
    Process.kill(0, pid)
    true
  rescue Errno::ESRCH
    false
  rescue Errno::EPERM
    true
  end

快速开发技巧中稍作修改 你可能不想拯救 EPERM,意思是“它正在运行,但你不能杀死它”。

于 2019-08-28T08:09:41.763 回答