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?

Tom
  • 33,626
  • 31
  • 85
  • 109

7 Answers7

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.

Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
  • Thanks, but I can't find a .pid file on disk for the ruby proceses? – Tom Jan 11 '11 at 08:13
  • 5
    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: http://mmonit.com/wiki/Monit/FAQ#pidfile – Michael Kohl Jan 11 '11 at 08:41
  • 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. – Michael Kohl Jan 14 '11 at 20:01
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.

Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
  • Do you have an example usage of Daemons? – Tom Jan 18 '11 at 12:09
  • 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. – Michael Kohl Jan 19 '11 at 07:58
  • 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. – Maurício Linhares Jan 19 '11 at 11:53
  • 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 :-) – Michael Kohl Jan 19 '11 at 19:57
  • 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. – philipkd Mar 01 '12 at 23:13
  • The issue with monit is probably related to your PATH variable, make sure you're using the full path for all commands you need. – Maurício Linhares Mar 02 '12 at 17:26
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

Eki Eqbal
  • 5,779
  • 9
  • 47
  • 81
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

Kelvin
  • 1
  • 2
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)?

cam
  • 14,192
  • 1
  • 44
  • 29
0

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

yawn
  • 8,014
  • 7
  • 29
  • 34
0

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

Flyounet
  • 105
  • 1
  • 6
  • I'm using a tiny VPS, Gods memory usage seemed to be more expensive than I could afford. – Tom Jan 20 '11 at 18:17