centos redis 自动重启
配置init脚本
对于Centos,有一份https://gist.github.com/1335694
经过修改,如下:
- ##########################
- PATH=/usr/local/bin:/sbin:/usr/bin:/bin
- REDISPORT=6379
- EXEC=/usr/local/bin/redis-server
- REDIS_CLI=/usr/local/bin/redis-cli
- PIDFILE=/var/run/redis.pid
- CONF="/etc/redis.conf"
- case "$1" in
- start)
- if [ -f $PIDFILE ]
- then
- echo "$PIDFILE exists, process is already running or crashed"
- else
- echo "Starting Redis server..."
- $EXEC $CONF
- fi
- if [ "$?"="0" ]
- then
- echo "Redis is running..."
- fi
- ;;
- stop)
- if [ ! -f $PIDFILE ]
- then
- echo "$PIDFILE does not exist, process is not running"
- else
- PID=$(cat $PIDFILE)
- echo "Stopping ..."
- $REDIS_CLI -p $REDISPORT SHUTDOWN
- while [ -x ${PIDFILE} ]
- do
- echo "Waiting for Redis to shutdown ..."
- sleep 1
- done
- echo "Redis stopped"
- fi
- ;;
- restart|force-reload)
- ${0} stop
- ${0} start
- ;;
- *)
- echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
- exit 1
- esac
- ##############################
把上述代码存为redis,放到/etc/init.d/下面
chmod +x /etc/init.d/redis
其实做成服务启动,也是调用redis-server,如果想让它在后台作为daemon运行,那么
需要修改redis.conf,将 daemonize no 改为 daemonize yes
4,设定开机启动服务
sudo chkconfig redis on
5,启动,停止redis
service redis start #或者 /etc/init.d/redis start
service redis stop #或者 /etc/init.d/redis stop
6,测试redis
- redis-cli
- redis 127.0.0.1:6379> set foo 123
- OK
- redis 127.0.0.1:6379> get foo
- "123"
- redis 127.0.0.1:6379> exit