Redis设置为开机自启动

database

1. 新建一个文件

vim /etc/init.d/redis

2. 将下面内容粘贴到文件中

#!/bin/sh

#chkconfig: 2345 90 10

#description: Redis is a persistent key-value database

PATH=/usr/local/bin:/sbin:/usr/bin:/bin

REDISPORT=6379

EXEC=/usr/local/bin/redis-server

REDIS_CLI=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_6379.pid

CONF="/myredis/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

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 SHUTDOWN

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

3. 修改下面配置为自己对应的信息

REDISPORT=6379 # 端口号

EXEC=/usr/local/bin/redis-server # 执行脚本的地址

REDIS_CLI=/usr/local/bin/redis-cli # 客户端执行脚本的地址

PIDFILE=/var/run/redis_6379.pid # 进程id文件地址,启动redis后才能看见

CONF="/myredis/redis.conf" #配置文件地址

4. 设置权限

chmod 755 /etc/init.d/redis

5. 启动测试

/etc/init.d/redis start

6. 启动成功会提示如下信息

Starting Redis server...

Redis is running...

7. 设置开机自启动

chkconfig --add /etc/init.d/redis

chkconfig redis on

* 如果启动失败

查看第一步的配置文件信息,是否和第二步的文件信息一致,vim粘贴会出现粘贴不全的问题

以上是 Redis设置为开机自启动 的全部内容, 来源链接: utcz.com/z/532856.html

回到顶部