一、创建 redis 服务配置文件
#!/bin/sh # chkconfig: 2345 10 90 # description: Start and Stop redis PATH=/usr/local/bin:/sbin:/usr/bin:/bin #找到本机安装redis后,存放redis命令的目录 REDISPORT=6379 #redis的默认端口, 要和下文中的redis.conf中一致 EXEC=/usr/local/redis/bin/redis-server #redis服务端的命令 REDIS_CLI=/usr/local/redis/bin/redis-cli #redis客户端的命令 这两个一般都在 PATH目录下 PIDFILE=/var/run/redis.pid #reids的进程文件生成的位置 CONF="/usr/local/redis/etc/redis.conf" #redis的配置文件所在的目录 #AUTH="1234" 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 exists, process is not running." else PID=$(cat $PIDFILE) echo "Stopping..." $REDIS_CLI -p $REDISPORT SHUTDOWN sleep 2 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/init.d/redis {start|stop|restart|force-reload}" >&2 exit 1 esac
二、给文件授权
chmod 777 /etc/init.d/redis
三、添加到服务列表
chkconfig --add redis
四、设为开机启动
chkconfig redis on
五. 启动/停止服务
service redis start service redis stop
如果执行service redis stop ,出现提示/var/run/redis_6379.pid does not exist, process is not running 这说明在run下没有这个文件,我们进入到/var/run下发先真的没有这个文件,但是有redis.pid文件。
所以我们要将/etc/rc.d/init.d/redis 文件中的redis_${REDISPORT}.pid修改为redis.pid。