1

Vagrant環境にCentOSをインストールし、monitでsidekiqを監視したものの、プロセスの停止は感知できますが、monitからsidekiqを立ち上げることができません(sidekiq faled to startとなる)

なお、monitを使わずに手動でrailsのルートディレクトリに移動した後(cd /var/www/my_app)sidekiqの起動コマンド(bundle exec sidekiq --pidfile /var/www/my_app/tmp/pids/sidekiq.pid --logfile /var/www/my_app/log/sidekiq.log --daemon)をたたくと起動することができます。

monitのバージョンと設定ファイルは以下のとおりなのですが、原因がわからず、困っています。

■ バージョン情報

$ cat /etc/redhat-release
CentOS release 6.6 (Final)

$ monit -V
This is Monit version 5.5

$ sidekiq -V
Sidekiq 3.3.4

■ /etc/monit.conf

set daemon  60 
set logfile /var/log/monit.log
set statefile /var/monit/state
set httpd port 2812 and
    use address localhost  # only accept connection from localhost
    allow localhost        # allow localhost to connect to the server and
include /etc/monit.d/*

■ /etc/monit.d/sidekiq

check process sidekiq 
  with pidfile /var/www/my_app/tmp/pids/sidekiq.pid
  every 2 cycle
  start program = "/bin/bash -l -c 'cd /var/www/my_app && bundle exec sidekiq --pidfile /var/www/my_app/tmp/pids/sidekiq.pid --logfile /var/www/my_app/log/sidekiq.log --daemon'"
  stop  program = "/bin/bash -l -c 'cd /var/www/my_app && bundle exec sidekiqctl stop /var/www/my_app/tmp/pids/sidekiq.pid 10'"

■ monit.log
[GMT+1 Jun 14 02:57:24] error : 'sidekiq' process is not running
[GMT+1 Jun 14 02:57:24] info : 'sidekiq' trying to restart
[GMT+1 Jun 14 02:57:24] info : 'sidekiq' start: /bin/bash
[GMT+1 Jun 14 02:57:54] error : 'sidekiq' failed to start ← これを解消したい

■ sidekiq.log
何も出力されない

もし、なにかご存知の方いらっしゃいましたら、ご教示頂ければ幸いです。

2
  • 失敗理由があると思いますので、sidekiq の log を追記されるのはいかがでしょうか? 2015年6月14日 4:43
  • コメントありがとうございます。それがsidekiq.logには何もはかれないないんですよね... 一応その旨を追記しておきました。 2015年6月14日 4:52

2 件の回答 2

2

ターミナルから手動で実行した時には上手く動いて、monitから実行した時には動かないということは、環境の違いにより、起動コマンドがエラーになるからだと思います。monitのデバッグ方法を2つ紹介します。

  1. monitの環境をシュミレートしてデバッグ
  2. monitの起動コマンドでログを出力

1. monitの環境をシュミレートしてデバッグ

# monit runs as superuser
$ sudo su

# the -i option ignores the inherited environment
# this PATH is what monit supplies by default
$ env -i PATH=/bin:/usr/bin:/sbin:/usr/sbin /bin/sh

# try running start/stop program here
$ ここであなたのスクリプトを実行

2. monitの起動コマンドでログを出力

コマンドのラッパースクリプト

$ cat monit-wrapper.sh

#!/bin/sh
{
  echo "MONIT-WRAPPER date"
  date
  echo "MONIT-WRAPPER env"
  env
  echo "MONIT-WRAPPER $@"
  $@
  R=$?
  echo "MONIT-WRAPPER exit code $R"
} >/tmp/monit.log 2>&1

monitの設定

start program = "/path/to/monit-wrapper.sh YOUR-START-SCRIPT AND ARGS"
stop program = "/path/to/monit-wrapper.sh YOUR-STOP-SCRIPT AND ARGS"

参考サイト
https://stackoverflow.com/questions/3356476/debugging-monit
http://qiita.com/ogin_s57/items/370743d280ac216bf7c1

1
  • デバッグ方法を知らなかったので、非常に参考になりました! ありがとうございます! 2015年6月15日 16:15
1

私が以前にmonitを設定した時は、Rails3+rvm環境だったのですが、その時は switch user してから、起動スクリプトを実行しました。

check process delayed_job with pidfile /u/apps/thub/shared/pids/delayed_job.pid
  start program = "/bin/su - YOUR_APP_USER -c 'cd /path/to/app/current; RAILS_ENV=production script/delayed_job start'" 
  stop program = "/bin/su - YOUR_APP_USER -c 'cd /path/to/app/current; RAILS_ENV=production script/delayed_job stop'"
1
  • ユーザ切り替えたらいけました!ありがとうございます! 2015年6月15日 16:15

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。