Skip to content

Instantly share code, notes, and snippets.

@bobbydavid
Created April 29, 2012 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobbydavid/2553077 to your computer and use it in GitHub Desktop.
Save bobbydavid/2553077 to your computer and use it in GitHub Desktop.
Simple init.d script I wrote for starting/stopping mongod
#! /bin/sh
#
# Simple init.d script I wrote for starting/stopping mongod.
# Copyright (c) 2012 Robert Martin
#
# Filename: /etc/init.d/mongod
# (so rename it from mongod.sh to mongod)
#
### BEGIN INIT INFO
# Provides: mongod
# Required-Start: $local_fs $network $named $syslog $time
# Required-Stop:
# Should-Stop: halt reboot kexec
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: daemon for servicing mongo db requests
### END INIT INFO
. /lib/lsb/init-functions
DAEMON=/usr/local/bin/mongod
LOGFILE=/usr/local/log/mongodb.log
DATABASE=/usr/local/mongodb
OTHER_OPTS='--journal'
#####################################
DAEMON_OPTS="--fork --dbpath $DATABASE \
--logpath $LOGFILE $OTHER_OPTS --logappend"
# Check for existence of executable
if ! [ -x $DAEMON ]; then
log_failure_msg "Cannot find executable: $DAEMON"
exit 1
fi
do_start () {
log_action_msg "Starting MongoDB Daemon"
}
do_stop () {
log_action_msg "Stopping MongoDB Daemon"
start-stop-daemon --exec $DAEMON --stop
return $?
}
is_started () {
start-stop-daemon --test --quiet --exec $DAEMON --start
if [ $? -eq 0 ]; then
return 1
fi
return 0
}
do_restart () {
do_stop
ERR=$?
if [ $ERR -ne 0 ]; then
return $ERR
fi
do_start
return $?
}
# 0 => Started; 1 => Stopped
do_status () {
if is_started; then
log_daemon_msg "'$DAEMON' is started."
return 0
fi
log_daemon_msg "'$DAEMON' is stopped."
return 1
}
case "$1" in
start)
do_start
;;
restart)
do_restart
;;
stop)
do_stop
;;
status)
do_status
;;
*)
echo "Usage: $0 start|stop|restart|status" >&2
exit 3
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment