Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for a running process with Ruby?

I use a scheduler (Rufus scheduler) to launch a process called "ar_sendmail" (from ARmailer), every minute.

The process should NOT be launched when there is already such a process running in order not to eat up memory.

How do I check to see if this process is already running? What goes after the unless below?

scheduler = Rufus::Scheduler.start_new

  scheduler.every '1m' do

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

  end

end
like image 745
TomDogg Avatar asked Jan 04 '11 13:01

TomDogg


People also ask

How do I find out what processes are running with PID?

The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.

How do you check if a process is running using Java?

If you want to check the work of java application, run 'ps' command with '-ef' options, that will show you not only the command, time and PID of all the running processes, but also the full listing, which contains necessary information about the file that is being executed and program parameters.

What is Ruby process?

A Ruby Process is the instance of an application or a forked copy. In a traditional Rails application, each Process contains all the build up, initialization, and resource allocation the app will need.


2 Answers

unless `ps aux | grep ar_sendmai[l]` != ""
like image 109
stef Avatar answered Sep 24 '22 00:09

stef


This looks neater I think, and uses built-in Ruby module. Send a 0 kill signal (i.e. don't kill):

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

Slightly amended from Quick dev tips You might not want to rescue EPERM, meaning "it's running, but you're not allowed to kill it".

like image 25
Mike Avatar answered Sep 26 '22 00:09

Mike