#!/bin/sh

set -e
set -o xtrace

cat > /etc/init/mongod.conf << '_EOF_'
limit fsize unlimited unlimited
limit cpu unlimited unlimited
limit as unlimited unlimited
limit nofile 64000 64000
limit rss unlimited unlimited
limit nproc 64000 64000

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

pre-start script
  mkdir -p /var/run/mongodb/
  touch /var/run/mongodb/mongod.pid
  chown mongodb -R /var/run/mongodb/
end script

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

script
  CONF=/etc/mongod.conf

  # 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

  exec start-stop-daemon --start \
        --chuid mongodb \
        --pidfile /var/run/mongod.pid \
        --make-pidfile \
        --exec $NUMACTL /usr/bin/mongod $DAEMON_OPTS
end script
_EOF_
