CentOS下redis自启动shell脚本

用这个脚本管理之前,需要先配置下面的内核参数,否则Redis脚本在重启或停止redis时,将会报错,并且不能自动在停止服务前同步数据到磁盘上:

复制代码 代码如下:

# vi /etc/sysctl.conf

vm.overcommit_memory = 1


然后应用生效:
复制代码 代码如下:
# sysctl -p

然后增加服务并开机自启动:

复制代码 代码如下:

# chmod 755 /etc/init.d/redis
# chkconfig �Cadd redis
# chkconfig �Clevel 345 redis on
# chkconfig �Clist redis

脚本源码:

#!/bin/bash
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig:  - 80 12
# description: Redis is a persistent key-value database
# processname: redis-server
# config:   /usr/local/redis/etc/redis.conf
# pidfile:   /usr/local/redis/var/redis.pid

source /etc/init.d/functions

BIN="/usr/local/redis/bin"
CONFIG="/usr/local/redis/etc/redis.conf"
PIDFILE="/usr/local/redis/var/redis.pid"


### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"

RETVAL=0
prog="redis-server"
desc="Redis Server"

start() {

    if [ -e $PIDFILE ];then
       echo "$desc already running...."
       exit 1
    fi

    echo -n $"Starting $desc: "
    daemon $BIN/$prog $CONFIG

    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
    return $RETVAL
}

stop() {
    echo -n $"Stop $desc: "
    killproc $prog
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE
    return $RETVAL
}

restart() {
  stop
  start
}

case "$1" in
 start)
    start
    ;;
 stop)
    stop
    ;;
 restart)
    restart
    ;;
 condrestart)
    [ -e /var/lock/subsys/$prog ] && restart
    RETVAL=$?
    ;;
 status)
    status $prog
    RETVAL=$?
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|condrestart|status}"
    RETVAL=1
esac

exit $RETVAL

注意:请将脚本中redis的启动文件、配置文件和pid文件存放位置按你配置的位置做相应修改,文件保存的时候注意是unix下可运行的模式,否则在linux下运行的时候会提示错误,/bin/bash^M: bad interpreter: No such file or directory

你可能感兴趣的:(CentOS下redis自启动shell脚本)