Monit bundle exec rails s
Monit bundle exec rails s
我有以下 shell 脚本,它允许我启动我的 rails 应用程序,假设它称为 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 &
以上文件的权限为:
-rwxr-xr-x 1 user user 410 Mar 21 10:00 start-app.sh*
如果我想检查流程,我会执行以下操作:
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]
P.S: 我 grep ":4445" 的原因是因为我在不同端口上的进程很少 运行。 (针对不同的项目)
现在开始监视,我使用 apt-get 安装它,repo 的最新版本是 5.16,因为我 运行 在 Ubuntu 16.04,还要注意 monit 是 运行 作为 root,这就是我在下面指定 gid uid 的原因。 (因为启动脚本习惯于从 "user" 而不是 "root" 执行)
这里是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,显示如下:
[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 看到它不到 3 秒grep | grep ":4445",但此输出与我显示的上述输出不同,它显示正在执行的 shell 脚本的内容,特别是这个:
blablalba... nohup bundle exec rails s -e production -p 4445
然后它就消失了。然后它尝试一次又一次地重新执行 shell.. ...
我错过了什么,我的配置有什么问题?请注意,我无法更改 start-app.sh 中的任何内容,因为它正在生产并 100% 工作。 (我只是想监视它)
编辑:根据我的理解和经验,这似乎是一个环境变量问题或路径问题,但我不确定如何解决它,将环境变量放在 monit 中没有任何意义.. 如果其他人想要编辑该 shell 脚本或添加新内容怎么办?我希望你明白我的意思
如我所料,这是用户环境问题,我通过如下编辑 monit 配置解决了这个问题:
之前(不工作)
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)作为 "user" 因为它将 仅 执行 shell 脚本在 "user" 的名称中,但它不会 get/import/use 用户的 env 路径或 env 变量。
我有以下 shell 脚本,它允许我启动我的 rails 应用程序,假设它称为 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 &
以上文件的权限为:
-rwxr-xr-x 1 user user 410 Mar 21 10:00 start-app.sh*
如果我想检查流程,我会执行以下操作:
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]
P.S: 我 grep ":4445" 的原因是因为我在不同端口上的进程很少 运行。 (针对不同的项目)
现在开始监视,我使用 apt-get 安装它,repo 的最新版本是 5.16,因为我 运行 在 Ubuntu 16.04,还要注意 monit 是 运行 作为 root,这就是我在下面指定 gid uid 的原因。 (因为启动脚本习惯于从 "user" 而不是 "root" 执行)
这里是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,显示如下:
[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 看到它不到 3 秒grep | grep ":4445",但此输出与我显示的上述输出不同,它显示正在执行的 shell 脚本的内容,特别是这个:
blablalba... nohup bundle exec rails s -e production -p 4445
然后它就消失了。然后它尝试一次又一次地重新执行 shell.. ... 我错过了什么,我的配置有什么问题?请注意,我无法更改 start-app.sh 中的任何内容,因为它正在生产并 100% 工作。 (我只是想监视它)
编辑:根据我的理解和经验,这似乎是一个环境变量问题或路径问题,但我不确定如何解决它,将环境变量放在 monit 中没有任何意义.. 如果其他人想要编辑该 shell 脚本或添加新内容怎么办?我希望你明白我的意思
如我所料,这是用户环境问题,我通过如下编辑 monit 配置解决了这个问题:
之前(不工作)
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)作为 "user" 因为它将 仅 执行 shell 脚本在 "user" 的名称中,但它不会 get/import/use 用户的 env 路径或 env 变量。