install_r45_on_ubuntu_1804l.../mongo_service.sh

102 lines
2.4 KiB
Bash

#!/bin/bash
case $1 in
start)
if [ -f "/var/lib/mongodb/mongod.lock" ]; then
exit 0
else
self_pid=$$
pid=`ps aux | grep mongod | grep -v 'service' | grep -v 'grep' | grep -v '/etc/init.d' | awk '{print $2}'`;
COUNTER=0
for i in $pid
do
if [ -f "/proc/$i/status" ]; then
echo "$i" >> /var/lib/mongodb/mongod.lock;
COUNTER=$((COUNTER+1))
fi
done
if [ $COUNTER == 0 ]; then
echo "starting mongo."
sudo mongod --config /etc/mongod.conf &
exit 0
else
echo "mongo is already running."
fi
fi
;;
stop)
echo "stoping mongo."
if [ -f "/var/lib/mongodb/mongod.lock" ]; then
sudo kill -s TERM `cat /var/lib/mongodb/mongod.lock`
sudo rm -f /var/lib/mongodb/mongod.lock
else
pid=`ps aux | grep mongod | grep -v 'service' | grep -v 'grep' | grep -v '/etc/init.d' | awk '{print $2}'`;
for i in $pid
do
kill -s TERM $i
done
fi
;;
restart|reload)
echo "stoping mongo."
if [ -f "/var/lib/mongodb/mongod.lock" ]; then
sudo kill -s TERM `cat /var/lib/mongodb/mongod.lock`
sudo rm -f /var/lib/mongodb/mongod.lock
else
pid=`ps aux | grep mongod | grep -v 'service' | grep -v 'grep' | grep -v '/etc/init.d' | awk '{print $2}'`;
for i in $pid
do
kill -s TERM $i
done
fi
sudo mongod --config /etc/mongod.conf &
exit 0
;;
status)
if [ -f "/var/lib/mongodb/mongod.lock" ]; then
pid=`cat /var/lib/mongodb/mongod.lock`
COUNTER=0
ERRORPID=0
for i in $pid
do
if [ -f "/proc/$i/status" ]; then
COUNTER=$((COUNTER+1))
else
ERRORPID=1
fi
done
if [ $ERRORPID == 0 ] && [ $COUNTER != 0 ]; then
echo "mongo is already running"
else
COUNTER=0
pid=`ps aux | grep mongod | grep -v 'service' | grep -v 'grep' | grep -v '/etc/init.d' | awk '{print $2}'`;
cat /dev/null > /var/lib/mongodb/mongod.lock;
for i in $pid
do
if [ -f "/proc/$i/status" ]; then
echo "$i" >> /var/lib/mongodb/mongod.lock;
COUNTER=$((COUNTER+1))
fi
done
if [ $COUNTER == 0 ]; then
echo "mongo is not running."
else
echo "mongo is already running."
fi
fi
else
COUNTER=0
pid=`ps aux | grep mongod | grep -v 'service' | grep -v 'grep' | grep -v '/etc/init.d' | awk '{print $2}'`;
for i in $pid
do
if [ -f "/proc/$i/status" ]; then
echo "$i" >> /var/lib/mongodb/mongod.lock;
COUNTER=$((COUNTER+1))
fi
done
if [ $COUNTER == 0 ]; then
echo "mongo is not running."
else
echo "mongo is already running."
fi
fi
esac