redis环境搭建脚本

#!/bin/bash

# 准备redis包
cd /usr/local/src || exit
wget http://download.redis.io/releases/redis-3.2.11.tar.gz

if [ $? -ne 0 ] ; then
  echo "下载redis安装包失败!"
  exit 1
fi

# 编译安装
tar zvxf redis-3.2.11.tar.gz
cd redis-3.2.11 | exit
make PREFIX=/usr/local/redis install

# 复制配置文件到安装目录下,并修改
cp ./redis.conf  /usr/local/redis/redis.conf
sed -i '128s/.*/daemonize yes/' /usr/local/redis/redis.conf
sed -i '480s/.*/requirepass Redis/' /usr/local/redis/redis.conf

# 启动
/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf

if [ $? -ne 0 ] ; then
    echo "redis启动异常!"
    exit 1
fi

# 添加开机启动
# 如果不是脚本,手动操作的可以,可以将线程的启动脚本复制过去,然后按照下面的修改
# cp /usr/local/src/redis-3.2.11/utils/redis_init_script   /etc/init.d/redis
echo '#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database
REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/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
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -a "Redis" -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac
' > /etc/init.d/redis
chmod +x /etc/init.d/redis
chkconfig redis on

# 添加到环境变量
sed -i '$a export PATH=/usr/local/redis/bin:$PATH' /etc/profile
source /etc/profile


# 接下来自己可以使用service启停进行测试

 

posted @ 2019-03-30 22:11  dalichenpingan  阅读(136)  评论(0编辑  收藏  举报