首页 > 代码库 > 【redis运维】redis自动安装脚本(只安装redis)
【redis运维】redis自动安装脚本(只安装redis)
Redis自动安装部署
本文总共分为4个部分:
redis自动安装脚本目录结构说明;
redis自动安装脚本内容;
redis的操作系统服务脚本说明;
本文引用的资料链接地址。
说明:本文未涉及redis内存分配器部分的内容。
第一部分
Redis自动安装脚本目录结构说明:
# ls 查看当前目录包含以下3个文件
-- install_redis.sh # redis自动安装脚本
-- redis-2.8.9.tar.gz # redis安装包(需make,make test,make install)
-- redis_service_script # redis 操作系统服务脚本(用于配置redis服务开机自启动,以及redis服务管理命令)
第二部分
###########################redis自动安装脚本内容开始(install_redis.sh)######################
#!/bin/bash
# Version: v1.0 by JiangYanguo on 2014-05-14
# Desc: auto make, make install and configure redis
REDIS_CONF="/etc/redis.conf"
# 检查redis的安装包是否存在
if [ "`ls redis-*.tar.gz >/dev/null 2>&1 ; echo $?`" -ne 0 ]
then
echo "Error: redis*.tar.gz file doesn‘t exists."
exit
else
REDIS_PACKAGE="`ls -ltr redis-*.tar.gz | head -1 | awk ‘{print $NF}‘ `"
fi
# 安装redis,make,make test,make install
# install redis
echo "Info: install redis..."
tar zxf ${REDIS_PACKAGE}
REDIS_DIR="`ls -ltr | egrep ‘^d‘ | awk ‘{print $NF}‘`"
cd ${REDIS_DIR}
pwd
make
make test
if [ "$?" -nt 0 ];then
echo "Some dependent packages are needed."
exit
fi
make install
echo "Info: set ${REDIS_CONF} ..."
cd ..
# 设置redis配置文件,data目录为/home/redis
# set /etc/redis.conf
cp redis.conf /etc/
sed -i ‘s/^daemonize\(.*\)/#daemonize\1/g‘ ${REDIS_CONF}
sed -i ‘2 adaemonize yes‘ ${REDIS_CONF}
sed -i ‘s/^dir\(.*\)/#dir\1/g‘ ${REDIS_CONF}
sed -i ‘2 adir \/home\/redis\/‘ ${REDIS_CONF}
# set memory allocate method
[ "`egrep ‘vm.overcommit_memory=1‘ /etc/sysctl.conf | wc -l`" -eq 0 ] && echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
/sbin/sysctl -p
# add redis user
useradd redis
# set redis data dir
mkdir -p /home/redis/
chown -R redis.redis /home/redis
#设置redis服务开机自启动,也是redis的管理服务
# set redis service
echo "Info: set redis service ..."
cp redis_service_script /etc/init.d/redis
chmod +x /etc/init.d/redis
chkconfig --add redis
chkconfig redis on
#
echo "Info: finish installing redis."
echo ""
########################## redis自动安装脚本内容结束 (install_redis.sh)#####################################
第三部分
########################## redis的操作系统服务脚本内容开始(redis_service_script)#########################################
#!/bin/bash
# redis - Startup script for redis
# chkconfig: 2345 85 152345 85 15
# description: startup script for redis
# processname: redis-server
# config: /etc/redis.conf
# pidfile: /var/run/redis.pid
# datafilepath: /home/redis/
#
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli
PIDFILE="/var/run/redis.pid"
CONF="/etc/redis.conf"
## start
function start(){
echo "Info: start redis..."
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
function stop(){
echo "Info: stop redis..."
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
function restart(){
stop
echo ""
start
echo ""
}
## info
function info(){
echo "Info: redis info..."
if [ -f $PIDFILE ];then
$REDIS_CLI info
else
echo "$PIDFILE does not exist, process is not running"
fi
}
## status
function status(){
REDIS_SERVER_COUNT="`netstat -nltp | grep redis-server | wc -l`"
if [ -f $PIDFILE ] && [ "${REDIS_SERVER_COUNT}" -gt 0 ]
then
echo "Info: redis is running."
elif [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
elif [ "${REDIS_SERVER_COUNT}" -eq 0 ]
then
echo "Redis is not listening any port."
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
info)
info
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|info|status}"
esac
########################## redis的操作系统服务脚本内容结束(redis_service_script)#########################################
第四部分
本文参考的资料链接地址:
Redis安装: http://www.cnblogs.com/zhuhongbao/archive/2013/06/04/3117997.html
Redis安装: http://blog.chinaunix.net/uid-790245-id-3766268.html