Monitor ruby processes with Monit

别来无恙 提交于 2019-12-01 02:47:43

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.

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.

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

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)?

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!