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
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.
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.
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.
unless `ps aux | grep ar_sendmai[l]` != ""
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With