redis设置开机启动
方式一
1.设置redis.conf中daemonize为yes,确保守护进程开启,也就是在后台可以运行.
#vi编辑redis安装目录里面的redis.conf文件 [root@localhost /]# vi /usr/redis/redis-3.2.4/redis.conf
2.复制redis配置文件(启动脚本需要用到配置文件内容,所以要复制)
#1.在/etc下新建redis文件夹 [root@localhost /]# mkdir /etc/redis #2.把安装redis目录里面的redis.conf文件复制到/etc/redis/6379.conf里面,6379.conf是取的文件名称,启动脚本里面的变量会读取这个名称,所以要是redis的端口号改了,这里也要修改 [root@localhost redis]# cp /usr/redis/redis-3.2.4/redis.conf /etc/redis/6379.conf
3.复制redis启动脚本
#1.redis启动脚本一般在redis根目录的utils,如果不知道路径,可以先查看路径 [root@localhost redis]# find / -name redis_init_script /usr/redis/redis-3.2.4/utils/redis_init_script #2.复制启动脚本到/etc/init.d/redis文件中 [root@localhost redis]# cp /usr/redis/redis-3.2.4//utils/redis_init_script /etc/init.d/redis
4.修改启动脚本参数
[root@localhost redis]# vi /etc/init.d/redis #在/etc/init.d/redis文件的头部添加下面两行注释代码,也就是在文件中#!/bin/sh的下方添加 # chkconfig: 2345 10 90 # description: Start and Stop redis
如图
同时还要修改参数,指定redis的安装路径
修改完成后按 Esc ,再按 :wq + Enter(回车) 保存并退出
修改完成后的截图
5.启动redis
打开redis命令:service redis start
关闭redis命令:service redis stop
设为开机启动:chkconfig redis on
设为开机关闭:chkconfig redis off
方式二
1.编写脚本
[root@localhost ~]# vi /etc/init.d/redis
复制下面代码到脚本中(注意要修改里面redis的安装路径,以/usr/redis/redis-3.2.4路径为例)(这段代码就是redis根目录 /utils/redis_init_script 启动脚本的代码)
#!/bin/sh # chkconfig: 2345 10 90 # description: Start and Stop redis REDISPORT=6379 EXEC=/usr/redis/redis-3.2.4/src/redis-server CLIEXEC=/usr/redis/redis-3.2.4/src/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pid CONF="/usr/redis/redis-3.2.4/redis.conf"case "$1" in start)if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed"elseecho "Starting Redis server..." $EXEC $CONF & fi ;; stop)if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running"elsePID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdownwhile [ -x /proc/${PID} ]doecho "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; restart) "$0" stop sleep 3 "$0" start ;; *) echo "Please use start or stop or restart as first argument" ;; esac
3.保存退出,设置权限
[root@localhost ~]# chmod 777 /etc/init.d/redis
4.启动redis
[root@localhost ~]# service redis start
赞 (0)