3

我写了一个脚本,可以很好地启动和停止服务器。

#!/bin/bash

PID_FILE='/var/run/rserve.pid'

start() {
       touch $PID_FILE
       eval "/usr/bin/R CMD Rserve"
       PID=$(ps aux | grep Rserve | grep -v grep | awk '{print $2}')
       echo "Starting Rserve with PID $PID"
       echo $PID > $PID_FILE
}
stop () {
       pkill Rserve
       rm $PID_FILE
       echo "Stopping Rserve"
}

case $1 in
    start)
       start
       ;;
    stop)  
       stop
       ;;
     *)  
       echo "usage: rserve {start|stop}" ;;
 esac
 exit 0

如果我通过运行启动它

rserve start

然后启动monit它会正确捕获PID和服务器:

The Monit daemon 5.3.2 uptime: 0m 

Remote Host 'localhost'
  status                            Online with all services
  monitoring status                 Monitored
  port response time                0.000s to localhost:6311 [DEFAULT via TCP]
  data collected                    Mon, 13 May 2013 20:03:50

System 'system_gauss'
  status                            Running
  monitoring status                 Monitored
  load average                      [0.37] [0.29] [0.25]
  cpu                               0.0%us 0.2%sy 0.0%wa
  memory usage                      524044 kB [25.6%]
  swap usage                        4848 kB [0.1%]
  data collected                    Mon, 13 May 2013 20:03:50

如果我停止它,它将正确终止该进程并取消对其进行监控。但是,如果我再次启动它,它将不会再次启动服务器:

ps ax | grep Rserve | grep -vc grep
1
monit stop localhost
ps ax | grep Rserve | grep -vc grep
0
monit start localhost

[UTC May 13 20:07:24] info     : 'localhost' start on user request
[UTC May 13 20:07:24] info     : monit daemon at 4370 awakened
[UTC May 13 20:07:24] info     : Awakened by User defined signal 1
[UTC May 13 20:07:24] info     : 'localhost' start: /usr/bin/rserve
[UTC May 13 20:07:24] info     : 'localhost' start action done
[UTC May 13 20:07:34] error    : 'localhost' failed, cannot open a connection to INET[localhost:6311] via TCP

这是监视器:

check host localhost with address 127.0.0.1
  start = "/usr/bin/rserve start"
  stop = "/usr/bin/rserve stop"
  if failed host localhost port 6311 type tcp with timeout 15 seconds for 5 cycles
    then restart
4

5 回答 5

8

我也有通过 shell 启动或停止进程的问题。一种解决方案可能是在配置中添加“/bin/bash”,如下所示:

start program = "/bin/bash /urs/bin/rserv start"
stop program = "/bin/bash /urs/bin/rserv stop"

它对我有用。

于 2013-10-25T08:24:52.310 回答
4

monit是一个沉默的杀手。它什么也没告诉你。以下是我会检查哪些监视器无法帮助您识别的内容

  1. 检查您正在读取/写入的所有文件的权限。如果要将输出重定向到文件,请确保该文件可由用于执行程序的 uid 和 gid 写入
  2. 再次检查您尝试运行的程序的 exec 权限
  3. 指定您尝试执行的任何程序的完整路径(并非绝对必要,但如果您始终指定完整路径,则不必担心未设置路径)
  4. 在尝试调查 monit 未启动的原因之前,请确保您可以在 monit 之外运行该程序而不会出现任何错误。
于 2014-08-15T20:13:55.530 回答
1

如果监视器日志正在显示

failed to start (exit status -1) -- no output

那么可能是您尝试在没有任何 Bash 基础架构的情况下运行脚本。您可以通过将其包装在 中来运行这样的命令/bin/bash -c,如下所示:

check process my-process
  matching "my-process-name"
  start program = "/bin/bash -c '/etc/init.d/my-init-script'"
于 2018-08-22T04:46:09.120 回答
0

当 monit 启动时,它会检查自己的 pidfile 并检查具有匹配 PID 的进程是否已经在运行 - 如果是,那么它只是唤醒这个进程。

在您的情况下,检查此 pid 是否被其他进程使用: ps -ef |grep 4370

如果是,那么您需要删除以下文件(通常在 /run 目录下)并再次启动monit:monit.pid

于 2017-04-27T23:01:30.190 回答
0

对我来说,问题是停止命令没有运行,即使我在配置中特别指定了“然后重新启动”。解决方案只是改变: start program = "/etc/init.d/.... restart"

于 2019-06-28T07:57:44.590 回答