mongodb 3.0.3 Ubuntu 14.04.2 AWS m3.medium Upstart PID不匹配

时间:2015-05-13 20:05:33

标签: mongodb ubuntu upstart

是否可以通过upstart运行mongod并通过start-stop-daemon或其他方式跟踪PID?

在ubuntu安装的mongodb文档页面上按照这些说明操作后:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org

ubuntu软件包的所有残余都已在此之前删除。

我现在通过Upstart在启动时有一个正在运行的mongod实例。但由于某些原因,Upstart和initctl不知道它。它启动正常,但initctl认为它处于停止/等待状态。

即便:

我的/etc/mongod.conf.yml:

storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true

systemLog:
  destination: file
  path: /var/log/mongodb/mongod.log
  logAppend: true
  logRotate: rename
  component:
    accessControl:
      verbosity: 2

net:
   bindIp: 127.0.0.1
   port: 27017

processManagement:
  fork: true

setParameter:
   enableLocalhostAuthBypass: false

security:
  authorization: disabled

我的/etc/init/mongod.conf upstart脚本(将mongodb.pid重命名为mongod.pid):

# Ubuntu upstart file at /etc/init/mongod.conf

# Recommended ulimit values for mongod or mongos
# See http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
#
limit fsize unlimited unlimited
limit cpu unlimited unlimited
limit as unlimited unlimited
limit nofile 64000 64000
limit rss unlimited unlimited
limit nproc 32000 32000

kill timeout 300 # wait 300s between SIGTERM and SIGKILL.

pre-start script
  DAEMONUSER=${DAEMONUSER:-mongodb}
  if [ ! -d /var/lib/mongod ]; then
    mkdir -p /var/lib/mongodb && chown mongodb:mongodb /var/lib/mongodb
  fi
  if [ ! -d /var/log/mongod ]; then
    mkdir -p /var/log/mongodb && chown mongodb:mongodb /var/log/mongodb
  fi

  touch /var/run/mongod.pid

  chown $DAEMONUSER /var/run/mongod.pid;

  if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
    echo never > /sys/kernel/mm/transparent_hugepage/enabled
  fi

  if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
    echo never > /sys/kernel/mm/transparent_hugepage/defrag
  fi

end script

start on runlevel [2345]
stop on runlevel [06]

script
  ENABLE_MONGOD="yes"
  CONF=/etc/mongod.conf.yml
  DAEMON=/usr/bin/mongod
  DAEMONUSER=${DAEMONUSER:-mongodb}

  if [ -f /etc/default/mongod ]; then . /etc/default/mongod; fi

  # Handle NUMA access to CPUs (SERVER-3574)
  # This verifies the existence of numactl as well as testing that the command works
  NUMACTL_ARGS="--interleave=all"
  if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null
  then
    NUMACTL="$(which numactl) -- $NUMACTL_ARGS"
    DAEMON_OPTS=${DAEMON_OPTS:-"--config $CONF"}
  else
    NUMACTL=""
    DAEMON_OPTS="-- "${DAEMON_OPTS:-"--config $CONF"}
  fi

  if [ "x$ENABLE_MONGOD" = "xyes" ]
  then
    exec start-stop-daemon --start \
        --chuid $DAEMONUSER \
        --pidfile /var/run/mongod.pid \
        --make-pidfile \
        --exec $NUMACTL $DAEMON $DAEMON_OPTS
  fi
end script

重启后我看到了:

$ ps aux | grep mongo
mongodb   1085  0.2  1.1 363764 46704 ?        Sl   11:57   0:06 /usr/bin/mongod --config /etc/mongod.conf.yml

一切似乎都很好。但是mongod.pid文件不存储与进程相同的pid:

$ cat /var/run/mongod.pid
985

应该是1085.

解决此问题的最佳方法是什么,以便Upstart可以访问实际的PID?

UPDATE:尝试添加expect daemonexpect fork行为有一些变化:initctl现在看到一个PID并表示mongod正在运行,但是有错误的PID。这意味着任何后续命令(如sudo stop mongodsudo start mongod)都会挂起。 fork或daemon似乎都没有解决这个问题;我错过了什么?

1 个答案:

答案 0 :(得分:0)

好吧,我脸上有点鸡蛋 - 我忽略了我闪亮的新/etc/mongod.conf.yml包含processManagement.fork: true的事实。将此设置为false允许start-stop-daemon捕获适当的PID。