Monit Bundle执行器Ra​​ils [英] Monit bundle exec rails s

查看:101
本文介绍了Monit Bundle执行器Ra​​ils的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下shell脚本,可用来启动我的Rails应用,假设它名为 start-app.sh :

I have the following shell script that allows me to start my rails app, let's say it's called start-app.sh:

#!/bin/bash

cd /var/www/project/current
. /home/user/.rvm/environments/ruby-2.3.3
RAILS_SERVE_STATIC_FILES=true RAILS_ENV=production nohup bundle exec rails s -e production -p 4445 > /var/www/project/log/production.log 2>&1 &

上面的文件具有以下权限:

the file above have permissions of:

-rwxr-xr-x  1 user user   410 Mar 21 10:00 start-app.sh*

如果我想检查过程,请执行以下操作:

if i want to check the process I do the following:

ps aux | grep -v grep | grep ":4445"

它会给我以下输出:

user  2960  0.0  7.0 975160 144408 ?       Sl   10:37   0:07 puma 3.12.0 (tcp://0.0.0.0:4445) [20180809094218]

PS:我之所以使用grep:4445"是因为我在不同端口上运行的进程很少. (用于不同的项目)

P.S: the reason i grep ":4445" is because i have few processes running on different ports. (for different projects)

现在要监视它,我使用apt-get进行安装,回购的最新版本是 5.16 ,因为我也在 Ubuntu 16.04 上运行请注意,monit以root身份运行,这就是为什么我在下面指定gid uid的原因. (因为开始脚本通常是从用户"而不是根"执行的)

now coming to monit, i used apt-get to install it, and the latest version from repo is 5.16, as i'm running on Ubuntu 16.04, also note that monit is running as root, that's why i specified the gid uid in the following. (because the start script is used to be executed from "user" and not "root")

这是monit的配置:

Here's the configuration for monit:

  set daemon 20            # check services at 20 seconds interval
  set logfile /var/log/monit.log
  set idfile /var/lib/monit/id
  set statefile /var/lib/monit/state


  set eventqueue
      basedir /var/lib/monit/events # set the base directory where events will be stored
      slots 100                     # optionally limit the queue size


  set mailserver xx.com port xxx
      username "xx@xx.com" password "xxxxxx"
      using tlsv12
      with timeout 20 seconds

  set alert xx@xx.com


  set mail-format {
       from: xx@xx.com
    subject: monit alert -- $EVENT $SERVICE
    message: $EVENT Service $SERVICE
                    Date:  $DATE
                  Action:  $ACTION
                    Host:  $HOST
             Description:  $DESCRIPTION
  }

 set limits {
        programOutput:     51200 B
        sendExpectBuffer:  25600 B
        fileContentBuffer: 51200 B
        networktimeout:    10 s
 }

  check system $HOST
    if loadavg (1min) > 4 then alert
    if loadavg (5min) > 2 then alert
    if cpu usage > 90% for 10 cycles then alert
    if memory usage > 85% then alert
    if swap usage > 35% then alert

check process nginx with pidfile /var/run/nginx.pid
        start program = "/bin/systemctl start nginx"
        stop program = "/bin/systemctl stop nginx"


check process redis
        matching "redis"
        start program = "/bin/systemctl start redis"
        stop program = "/bin/systemctl stop redis"

check process myapp
    matching ":4445"
    start program = "/bin/bash -c '/home/user/start-app.sh'" as uid "user" and gid "user"
    stop program = "/bin/bash -c /home/user/stop-app.sh" as uid "user" and gid "user"

   include /etc/monit/conf.d/*
   include /etc/monit/conf-enabled/*

现在,monit正在检测并警告我该进程何时关闭(如果我手动将其杀死)以及何时手动恢复,但它不会自动启动该Shell脚本..并根据/var/log/monit .log,它显示以下内容:

Now monit, is detecting and alerting me when the process goes down (if i kill it manually) and when it's manually recovered, but it won't start that shell script automatically.. and according to /var/log/monit.log, it's showing the following:

[UTC Aug 13 10:16:41] info     : Starting Monit 5.16 daemon
[UTC Aug 13 10:16:41] info     : 'production-server' Monit 5.16 started
[UTC Aug 13 10:16:43] error    : 'myapp' process is not running
[UTC Aug 13 10:16:46] info     : 'myapp' trying to restart
[UTC Aug 13 10:16:46] info     : 'myapp' start: /bin/bash
[UTC Aug 13 10:17:17] error    : 'myapp' failed to start (exit status 0) -- no output

到目前为止,当monit尝试执行脚本时,我看到的是它尝试加载该脚本(使用 ps aux | grep -v grep | grep:4445",我可以看到它的时间少于3秒. /strong>,但此输出与我显示的上述输出不同,它显示了正在执行的Shell脚本的内容,特别是以下内容:

So far what I see when monit tries to execute the script is that it tries to load it (i can see it for less than 3 seconds using ps aux | grep -v grep | grep ":4445", but this output is different from the above output i showed up, it shows the content of the shell script being executed and specifically this one:

blablalba... nohup bundle exec rails s -e production -p 4445

,然后消失.然后它会尝试重新执行Shell ..一次又一次... 我缺少什么,我的配置有什么问题?请注意,我无法更改 start-app.sh 中的任何内容,因为它已经投入生产并且可以100%正常工作. (我只想监视它)

and then it disappears. then it tries to re-execute the shell.. again and again... What am I missing, and what is wrong with my configuration? note that I can't change anything in the start-app.sh because it's on production and working 100%. (i just want to monitor it)

根据我的理解和经验,这似乎是环境变量问题或路径问题,但是我不确定如何解决它,将env变量放在monit中没有任何意义.如果其他人想要编辑该Shell脚本或添加新内容怎么办?我希望你明白我的意思

To my understanding and experience, it seems to be a Environment Variable issue or path issue, but i'm not sure how to solve it, it doesn't make any sense to put the env variables inside monit .. what if someone else wanted to edit that shell script or add something new? i hope you get my point

推荐答案

正如我所期望的,这是用户环境问题,我通过编辑如下的monit配置解决了该问题:

As i expected, it was user-environment issue and i solved it by editing monit configuration as below:

之前(无效)

check process myapp
    matching ":4445"
    start program = "/bin/bash -c '/home/user/start-app.sh'" as uid "user" and gid "user"
    stop program = "/bin/bash -c /home/user/stop-app.sh" as uid "user" and gid "user"


(工作后)

check process myapp
    matching ":4445"
    start program = "/bin/su -s /bin/bash -c '/home/user/start-app.sh' user"
    stop program = "/bin/su -s /bin/bash -c '/home/user/stop-app.sh' user"

说明:我从monit中删除了(uid和gid)作为用户" ,因为它将以用户"的名义执行shell脚本,但它赢了不能获取/导入/使用用户的环境路径或环境变量.

Explanation: i removed (uid and gid) as "user" from monit because it will only execute the shell script in the name of "user" but it won't get/import/use user's env path, or env variables.

这篇关于Monit Bundle执行器Ra​​ils的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆