下面我将为您详细讲解“Linux中设置Redis开机启动的方法”的完整攻略,以下是具体步骤:
1. 编写Redis启动脚本
在/etc/init.d/
目录下新建一个名为redis
的文件,这个文件就是我们的启动脚本,使用以下命令:
sudo vim /etc/init.d/redis
然后把以下代码粘贴进去:
#!/bin/sh
# chkconfig: 2345 95 05
# description: Redis is a persistent key-value database
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 Redis server..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
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
exit 0
在这段代码中,我们定义了启动Redis所需的一些变量以及需要执行的指令,可以根据自己的实际情况调整相关的参数和路径。
2. 赋予脚本权限
在安装好Redis之后,需要把这个启动脚本设置为可执行文件,并赋予它相应的权限。使用以下命令:
sudo chmod 755 /etc/init.d/redis
3. 使Redis自动启动
使用以下命令将Redis设置为自动启动:
sudo chkconfig --add redis
这个命令会自动将Redis添加到启动项中,达到自动启动的目的。当需要关闭自启动时,使用以下命令:
sudo chkconfig --del redis
4. 演示
以下是两个实例说明:
示例1:CentOS 7
假设我们的Redis安装位置是 /usr/local/bin/redis-server
,配置文件位置是 /etc/redis/redis.conf
。我们可以使用以下命令来演示如何使Redis自动启动:
sudo vim /etc/init.d/redis
将以下代码复制进去:
#!/bin/bash
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/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 Redis server..."
$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
${0} start
;;
*)
echo "Usage: $0 {start|stop|restart}" >&2
exit 1
esac
exit 0
接下来,使脚本可执行并且加入启动项:
sudo chmod 755 /etc/init.d/redis
sudo chkconfig --add redis
sudo chkconfig redis on
现在,重启系统,你会发现Redis已经自动启动了。
示例2:Ubuntu 16.04
假设我们的Redis安装位置是 /usr/bin/redis-server
,配置文件位置是 /etc/redis/redis.conf
。我们可以使用以下命令来演示如何使Redis自动启动:
首先,在/etc/init.d/
目录下新建一个名为redis
的文件:
sudo vim /etc/init.d/redis
将以下代码复制进去:
#! /bin/sh
### BEGIN INIT INFO
# Provides: redis-server
# Required-Start: $syslog $remote_fs
# Required-Stop: $syslog $remote_fs
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: redis-server - Persistent key-value db
# Description: redis-server - Persistent key-value db
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/redis-server
DAEMON_ARGS=/etc/redis/redis.conf
NAME=redis-server
DESC=redis-server
RUNDIR=/var/run/redis
PIDFILE=$RUNDIR/redis.pid
test -x $DAEMON || exit 0
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
mkdir -p $RUNDIR
touch $PIDFILE
chown redis:redis $RUNDIR $PIDFILE
chmod 755 $RUNDIR
if [ -f $PIDFILE ]
then
echo "already running."
exit 1
fi
# Protect against redis.conf changing in meantime
mkdir -p /var/lock/subsys
touch /var/lock/subsys/$NAME
$DAEMON $DAEMON_ARGS --pidfile $PIDFILE &
echo "started."
;;
stop)
echo -n "Stopping $DESC: "
if [ ! -f $PIDFILE ]
then
echo "not running."
exit 0
fi
rm -f /var/lock/subsys/$NAME
$DAEMON $DAEMON_ARGS --pidfile $PIDFILE --stop
echo "stopped."
;;
restart|force-reload)
${0} stop
sleep 1
${0} start
;;
*)
echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0
接下来,使脚本可执行并且加入启动项:
sudo chmod 755 /etc/init.d/redis
sudo update-rc.d redis defaults
现在,重启系统,你会发现Redis已经自动启动了。
以上就是在Linux上设置Redis开机启动的全部攻略,希望可以帮到您。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Linux中设置Redis开机启动的方法 - Python技术站