redis停机脚本带OOM配置功能
1.Shell
#!/bin/sh
#
# redis init file for starting up the redis daemon
#
# chkconfig: - 20 80
# description: Starts and stops the redis daemon.
#
# Source function library.
. /etc/rc.d/init.d/functions
name="redis-server"
exec="/opt/redis/$name"
cli="/opt/redis/redis-cli"
REDIS_CONFIG="/opt/redis/redis.conf"
pidfile=`grep "pidfile" ${REDIS_CONFIG}|cut -d ‘ ‘ -f2`
pidfile=${PID_FILE:=/var/run/redis.pid}
[ -e /etc/sysconfig/redis ] && . /etc/sysconfig/redis
lockfile=/var/lock/subsys/redis
shut(){
test x"$REDIS_DEBUG" != x && set -x
# Use awk to retrieve host, port from config file
HOST=`awk ‘/^[[:blank:]]*bind/ { print $2 }‘ $REDIS_CONFIG | tail -n1`
PORT=`awk ‘/^[[:blank:]]*port/ { print $2 }‘ $REDIS_CONFIG | tail -n1`
PASS=`awk ‘/^[[:blank:]]*requirepass/ { print $2 }‘ $REDIS_CONFIG | tail -n1`
SOCK=`awk ‘/^[[:blank:]]*unixsocket\s/ { print $2 }‘ $REDIS_CONFIG | tail -n1`
# Just in case, use default host, port
HOST=${HOST:-127.0.0.1}
PORT=${PORT:-6379}
# Setup additional parameters
# e.g password-protected redis instances
[ -z "$PASS" ] || ADDITIONAL_PARAMS="-a $PASS"
# shutdown the service properly
if [ -e "$SOCK" ] ; then
${cli} -s $SOCK $ADDITIONAL_PARAMS shutdown
else
${cli} -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
fi
}
start() {
[ -f $REDIS_CONFIG ] || exit 6
[ -x $exec ] || exit 5
retval=$?
echo -n $"Starting $name: "
daemon --user ${REDIS_USER-redis} "$exec $REDIS_CONFIG --daemonize yes --pidfile $pidfile"
echo
[ $retval -eq 0 ] && touch $lockfile || failure "Starting $name: "
return $retval
}
stop() {
echo -n $"Stopping $name: "
retval=$?
shut
sleep 2
if [ -f $pidfile ]
then
# shutdown haven‘t work, try old way
killproc -p $pidfile $name
retval=$?
else
success "$name shutdown"
fi
echo
[[ $retval -eq 0 ]] && rm -f $lockfile || failure "$name shutdown"
return $retval
}
set_oom() {
read PID < "$pidfile"
echo -n $"Setting $name PID:$PID oom_score_adj"
echo -1000 > /proc/${PID}/oom_score_adj
retval=$?
if [ $retval -ne 0 ];then
failure;echo;
#echo -n $" Setting $name PID:$PID oom_score_adj not success.";failure;echo
exit 0
else
success;echo;
fi
return $retval
}
restart() {
stop
start
}
rh_status() {
status -p $pidfile $name
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
rh_status_not_exits(){
rh_status_q
retval=$?
if [ $retval -ne 0 ] ;then
warning ;echo -n $"$name: Already Shutdown " ;echo
exit 0
fi
}
rh_status_exits(){
rh_status_q
retval=$?
if [ $retval -eq 0 ] ;then
warning ;echo -n $"$name: Already Running " ;echo
exit 0
fi
}
case "$1" in
start)
#rh_status_q && exit 0
rh_status_exits
$1
;;
stop)
#rh_status_q || exit 0
rh_status_not_exits
$1
;;
restart)
$1
;;
set_oom)
#rh_status_q || exit 0
rh_status_not_exits
$1
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|set_oom|condrestart|try-restart}"
exit 2
esac
exit $?
2.Go
原文地址:https://blog.51cto.com/zhangyc/2422116
时间: 2024-10-13 11:54:54