Monitor ruby processes with Monit
Asked Answered
B

7

13

I have a bunch of Ruby scripts which are long running, I'd like to ensure that every 30 seconds or so these are up.

I normally start the commands by simply ruby script-name.rb

How can I configure monit to look after these scripts?

Update: I tried to follow this method to create a wrapper script which would then launch the ruby process but it did not seem to create the .pid file and typing './wrapper-script stop' did nothing :/

Should I write the pid inside ruby or use a wrapper script to create the pid necessary for monit?

Biological answered 10/1, 2011 at 20:59 Comment(0)
R
6

The Monit Wiki has a lot of configuration examples:

http://mmonit.com/wiki/Monit/ConfigurationExamples

Just pick a simple one and modify it according to your needs.

Update: the wrapper script should create the pid for you in this line:

echo $$ > /var/run/xyz.pid;

Did you adapt the script to your needs? Is it executable (chmod +x)? Does it have write permissions for the destination? Maybe post the wrapper you are trying to use so I can help you more.

Retroact answered 10/1, 2011 at 22:16 Comment(3)
Thanks, but I can't find a .pid file on disk for the ruby proceses?Biological
Well, if your scripts don't create one, then there is no PID file. Either modify your script to create one or follow the FAQ entry: mmonit.com/wiki/Monit/FAQ#pidfileRetroact
Tom, are you making any progress on this? Did the links help? If not, maybe edit your question to let us know how far you got.Retroact
E
4

You don't need to write a wrapper script or try any kind of black magic, just use the Daemons library and you're done.

Imagine that you have a class Worker that has a method "run" that enters an infinite loop reading from a socket or anything like that, here's how you'd write your Daemons wrapper:

# this is file my_worker_control.rb
require 'rubygems'
require 'daemons'
require 'worker'

Daemons.run_proc(:app_name => 'my_worker', :dir_mode => :system, :log_output => true ) do
  Worker.run
end

Once the script is done, just call it from your command line or an init.d script:

my_worker_control.rb run|start|stop|restart

This config will generate a "my_worker.pid" file under "/var/run" and you can use monit to watch over the process by using this file.

Esmeralda answered 18/1, 2011 at 4:6 Comment(6)
Do you have an example usage of Daemons?Biological
In the ends that's just a wrapper script too, abstracted away in a Ruby gem. The bash version from the Monit Wiki isn't any more "black magic" than this.Retroact
Yep, but this wrapper script can be your "main" script on your ruby code, the one that starts it all, no need to deal with bash, write pid files, handle logging or anything like that. Daemons will handle all this stuff for you.Freed
Sure, I didn't mean to imply that "daemons" isn't useful, I've recommended it myself on SO. Anyway, let's just agree that it's a cool gem :-)Retroact
This is much easier to use. However, I'm getting frustrated by my script running on the command line vs. when monit runs it. It works when I run it, but when monit runs it, it runs, but there are bugs that didn't come up. It's extremely hard to debug, because I don't know what special conditions monit is creating that's messing my script up.Necklace
The issue with monit is probably related to your PATH variable, make sure you're using the full path for all commands you need.Freed
E
1

Modify the file :

/etc/init.d/skeleton 

You will need to slightly modify it, and then :

chmod +x /etc/init.d/process_name 
sudo update-rc.d process_name defaults
sudo /etc/init.d/process_name (start| stop| reload ) 

Now just use Monit with the pid at /var/run/process.pid

start location : sudo /etc/init.d/process start

stop location : sudo /etc/init.d/process stop

Cheers

Excerpta answered 7/11, 2012 at 15:15 Comment(0)
H
0

Writing the pid file in your ruby script may be easiest for you (just open a file and write $$ in it). That said, the wrapper script approach should work fine. Does your script have permission to write to a file in /var/run (or wherever you are putting the pidfile)?

Hafner answered 16/1, 2011 at 20:22 Comment(0)
R
0

As an alternative (to monit), have a look at bluepill.

Requisite answered 18/1, 2011 at 9:7 Comment(0)
R
0

(Surely out of subject but) as it is about ruby, why don't you use : http://god.rubyforge.org/ ?

Restrained answered 19/1, 2011 at 14:40 Comment(1)
I'm using a tiny VPS, Gods memory usage seemed to be more expensive than I could afford.Biological
M
0

Add this line to your ruby script yourapp.rb, that creates a pid file named yourapp.pid

File.open('/somepath/yourapp.pid', 'w') {|f| f.write Process.pid }

Configure Monit to check for the pid in /etc/monit/conf.d/yourapp

check process yourapp with pidfile /somepath/yourapp.pid

Module answered 18/9, 2014 at 18:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.