美文网首页Linux
将redis注册为service

将redis注册为service

作者: qiqj_ | 来源:发表于2017-06-04 22:50 被阅读0次

    1.将redis的配置文件复制到/etc/redis/redis.conf

      sudo cp /usr/local/redis/redis.conf /etc/redis/redis.conf
    

    2.创建并编辑/etc/rc.d/init.d/redis

     touch /etc/rc.d/init.d/redis
    
     vim /etc/rc.d/init.d/redis
    

    将一下脚本内容copy到/etc/rc.d/init.d/redis中

    # chkconfig:   2345 90 10
    
    # description:  Redis
    
    REDISPORT=6379
    EXEC=/usr/local/redis-3.2.1/src/redis-server
    REDIS_CLI=/usr/local/redis-3.2.1/src/redis-cli
    PIDFILE=/var/run/redis_6379.pid
    "CONF=""/etc/redis/redis.conf"""
    AUTH=123456 
    "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 -a $AUTH SHUTDOWN #根据redis的配置文件来编辑,此处是因为我自己的redis配置了认证密码
    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/rc.d/init.d/redis {start|stop|restart|force-reload}"" >&2"
    exit 1 
    esac 
    

    3.将redis注册为服务

    cd /etc/rc.d/init.d
    chmod +x redis #添加可执行权限
    chkconfig --add redis #注册为service
    chkconfig redis on  #开启
    

    4.服务开关命令

    • 开启redis服务 : service redis start
    • 停止redis服务 : service redis stop
    • 重启redis服务 : service redis restart

    相关文章

      网友评论

        本文标题:将redis注册为service

        本文链接:https://www.haomeiwen.com/subject/vlmbfxtx.html