方法一:
1.设置redis.conf中daemonize为yes,确保守护进程开启,也就是在后台可以运行
#vi编辑redis安装目录里面的redis.conf文件 [[email protected] /]# vi /usr/redis/redis-3.2.4/redis.con
2.复制redis配置文件(启动脚本需要用到配置文件内容,所以要复制)
#1.在/etc下新建redis文件夹 [[email protected] /]# mkdir /etc/redis #2.把安装redis目录里面的redis.conf文件复制到/etc/redis/6379.conf里面,6379.conf是取的文件名称,启动脚本里面的变量会读取这个名称,所以要是redis的端口号改了,这里也要修改 [[email protected] redis]# cp /usr/redis/redis-3.2.4/redis.conf /etc/redis/6379.conf
3.复制redis启动脚本
#1.redis启动脚本一般在redis根目录的utils,如果不知道路径,可以先查看路径 [[email protected] redis]# find / -name redis_init_script /usr/redis/redis-3.2.4/utils/redis_init_script #2.复制启动脚本到/etc/init.d/redis文件中 [[email protected] redis]# cp /usr/redis/redis-3.2.4//utils/redis_init_script /etc/init.d/redis
4.修改启动脚本参数
[[email protected] 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.编写脚本
[[email protected] ~]# 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" 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 ;; restart) "$0" stop sleep 3 "$0" start ;; *) echo "Please use start or stop or restart as first argument" ;; esac
3.保存退出,设置权限
[[email protected] ~]# chmod 777 /etc/init.d/redis
4.启动redis
[[email protected] ~]# service redis start
5. 重启
原文地址:https://www.cnblogs.com/moying-wq/p/10957501.html
时间: 2024-10-11 21:46:51