Kill MongoDB on OS X in a bash script

The documentation for MongoDB has a little gotcha On OS X the command
$ mongo --shutdown  is not supported.  

The way to work around this is good old fashioned unix geekery, to run ps, grep for [m]ongodb and then awk to just return the pid and not the pid and comm.
As shown below:


#!/bin/sh
echo "Script running in `dirname $0`"
echo "Present working directory $PWD"
TPROC="$(ps -A -o pid,comm | grep [m]ongodb | awk '{print$1}')"
if [ -z "$TPROC" ]
then
  echo "\$TPROC is null. So there is no running instance of mongod"
else
  echo "\$TPROC is NOT null."
  echo "$TPROC to be killed"
  kill $TPROC
fi
 

Comments