redis上配置自启动脚本

1.打开自启动脚本,路径为/redis/utils/redis_init_script,文件内容如下所示:

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.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 -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

2.一般执行过make  install 之后,redis-serrver和redis-cli等都默认处于/usr/local/bin/目录下,否则修改EXEC和FILEEXEC的配置,我们看CONF的配置我们知道需要在/etc下建一个redis的文件夹,然后再reids下建一个6379.conf的配置文件。

sudo   mkdir    /etc/redis

sudo   cp    /home/gl/pro/redis/redis.conf       /etc/redis/6379.conf

sudo    cp    /home/gl/pro/redis/redis_init_script     /etc/init.d/redisd


3.此时我们要创建一个服务redisd,设置为开机启动,redHat系类下可以用chkconfig命令,可是ubuntu下没有这个命令,我们用sysv-rc-conf来代替,首先需要安装这个命令,

sudo   apt   install    sysv-rc-conf

sysv-rc-confi     redisd   on


4.重启电脑就会看到redis服务已经自动启动了,可以直接redis-cli 了。


5.如果关闭自动启动服务,可以使用

sudo   sysv-rc-conf   redisd   off



6.除了sysv-rc-conf命令之外,还可以使用update-rc.d命令,此命令可以直接使用

sudo   update-rc.d    redisd   defaults


sudo  update-rc.d  -f   redisd   remove

你可能感兴趣的:(redis)