[root@MongoDB ~]# cat /etc/init.d/mongod
#!/bin/bash
MONGODIR=/usr/local/mongodb
MONGOD=$MONGODIR/bin/mongod
MONGOCONF=/etc/mongodb.conf
InfoFile=/tmp/start.mongo
. /etc/init.d/functions
status(){
PID = `awk 'NR == 2 {Print $ NF}' $ InfoFile`
Run_Num=`ps -p $PID|wc -l`
if [ $Run_Num -eq 2 ]; then
echo "MongoDB is running"
else
echo "MongoDB is shutdown"
return 3
be
}
start() {
status &>/dev/null
if [ $? -ne 3 ];then
action "start MongoDB, service operation ..." / bin / false
exit 2
be
$MONGOD -f $MONGOCONF
if [ $? -eq 0 ];then
action "启动MongoDB" /bin/true
else
action "start MongoDB" / bin / false
be
}
stop() {
$MONGOD -f $MONGOCONF --shutdown
if [ $? -eq 0 ];then
action "停止MongoDB" /bin/true
else
action "停止MongoDB" /bin/false
be
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 2
start
;;
status)
status
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 1
esac