[MongoDB]: To add mongod to Linux startup
mongod script:
#!/bin/bash
# mongod – Startup script for mongod
# description: Mongo is a scalable, document-oriented database.
# chkconfig: 35 85 15
MONGO_USER=mongod
if [ `id -nu` != $MONGO_USER -a `id -nu` != “root” ]; then
echo “Exiting – needs to be run as “root” or $MONGO_USER, not `id -nu`”
exit 5
fi
. /etc/rc.d/init.d/functions
CONFIGFILE=”/etc/mongod.conf”
DBPATH_TMP=`awk -F= ‘/^dbpath/{print $2}’ “$CONFIGFILE”`
DBPATH=`echo $DBPATH_TMP`
MONGOD=/opt/mongodb/bin/mongod
start()
{
if [ -f $DBPATH/mongod.lock ]; then
echo “A mongod process is already running, `cat $DBPATH/mongod.lock`”
echo “If the mongod process no longer exists MongoDB was probably shut down”
echo “uncleanly – this means the $DBPATH/mongod.lock lock file will”
echo “need to be removed before MongoDB can be restarted”
exit 4
fi
echo $”Starting mongod as user `id -nu` …”
if [ “`id -nu`” == “$MONGO_USER” ]; then
$MONGOD -f $CONFIGFILE
RETVAL=$?
else
runuser – $MONGO_USER -c “$MONGOD -f $CONFIGFILE”
RETVAL=$?
fi
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod
}
stop()
{
echo $”Stopping mongod: “
killproc -p “$DBPATH”/mongod.lock -d 300 $MONGOD
RETVAL=$?
rm -rf $DBPATH/mongod.lock
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mongod
}
restart () {
stop
start
}
case “$1” in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
if [ ! -f $DBPATH/mongod.lock ]; then
echo “mongod appears to not be running – no mongod.lock file found in $DBPATH”
RETVAL=3
else
echo “mongod appears to be running – using PID `cat $DBPATH/mongod.lock`”
RETVAL=0
fi
;;
*)
echo “Usage: $0 {start|stop|restart|status}”
RETVAL=1
esac
exit $RETVAL
To add the scripts in start-up, we can use any of the one following command.
In the below scenario, say you wanted add mongod start/stop/restart script to Linux ( reboot/start-up)
[root@dbversity init.d]# chkconfig –add mongod
[root@dbversity init.d]#
(OR)
Add a line in the /etc/rc.local as below.
[root@dbversity init.d]# cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don’t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/etc/init.d/mongod start
[root@dbversity init.d]#