Linux服务器上配置Redis服务开机自启动
Redis安装完毕后,正常启动命令:
./redis-server /etc/redis/redis-6379.conf
关闭命令:
./redis-cli -p 6379 -a pass shutdown
常用Linux开机启动配置的两种方法:
(1)编辑/etc/rc.local,添加开机启动运行命令;
(2)添加/etc/init.d/redis,通过chkconfig配置开机启动服务;
方法一:编辑/etc/rc.local,添加开机启动运行命令
第一步,将redis-server和redis-cli命令文件拷贝至/usr/local/bin目录:
cd /opt/servers/redis-3.0.7/src/
cp redis-server /usr/local/bin/
cp redis-cli /usr/local/bin/
第二步,编辑/etc/rc.local文件,文件内容最底下添加启动命令:
redis-server /etc/redis/redis-6379.conf
即可
方法二:添加/etc/init.d/redis脚本,通过chkconfig配置开机启动服务
第一步,同上,将redis-server和redis-cli命令文件拷贝至/usr/local/bin目录:
cd /opt/servers/redis-3.0.7/src/
cp redis-server /usr/local/bin/
cp redis-cli /usr/local/bin/
第二步:编辑启动脚本:/etc/init.d/redis
redis脚本具体内容如下:
#!/bin/sh
#chkconfig: 2345 20 90
#description: redis service
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis-${REDISPORT}.pid
CONF="/etc/redis/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 dose not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping..."
$CLIEXEC -p $REDISPORT -a '123456' shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
第二行注释#chkconfig: 2345 20 90说明:
chkconfig 运行级别 启动服务级别 停止服务级别
运行级别:
等级0表示:表示关机
等级1表示:单用户模式
等级2表示:无网络连接的多用户命令行模式
等级3表示:有网络连接的多用户命令行模式
等级4表示:不可用
等级5表示:带图形界面的多用户模式
等级6表示:重新启动
服务启动/停止级别:
0-100值; 值越小,级别越高
如果A服务需要依赖B服务(例:Java服务依赖Redis服务),则启动/停止级别可设置为:
A服务:#chkconfig 2345 80 50
B服务:#chkconfig 2345 50 80
即:
启动顺序:先启动B服务,后启动A服务;
停止顺序:先停止A服务,后停止B服务;
第三步,测试redis脚本
添加可执行权限:
chmod +x /etc/init.d/redis
测试脚本启动/关闭:
/etc/init.d/redis start
/etc/init.d/redis stop
通过查看redis进程,确认开启/关闭命令执行后状态
ps -ef|grep redis
[root@iZ2zegbfy ~]# /etc/init.d/redis start
Starting Redis Server...
[root@iZ2zegbfy ~]#
[root@iZ2zegbfy ~]# ps -ef|grep redis
root 9513 1 0 12:04 ? 00:00:00 /usr/local/bin/redis-server 127.0.0.1:6379
root 9519 9453 0 12:04 pts/6 00:00:00 grep --color=auto redis
[root@iZ2zegbfy ~]#
[root@iZ2zegbfy ~]# /etc/init.d/redis stop
Stopping...
Redis stopped
[root@iZ2zegbfy ~]#
[root@iZ2zegbfy ~]# ps -ef|grep redis
root 9577 9453 0 12:05 pts/6 00:00:00 grep --color=auto redis
[root@iZ2zegbfy ~]#
第四步:设置开机启动
设置开机启动命令:
cd /etc/init.d/
chkconfig redis on
服务启动/关闭命令:
service redis start
service redis stop
[root@iZ2zegbfy ~]# service redis stop
Stopping...
Redis stopped
[root@iZ2zegbfy ~]# ps -ef|grep redis
root 10125 10082 0 12:16 pts/7 00:00:00 grep --color=auto redis
[root@iZ2zegbfy ~]#
[root@iZ2zegbfy ~]# service redis start
Starting Redis Server...
[root@iZ2zegbfy ~]#
[root@iZ2zegbfy ~]# ps -ef|grep redis
root 10142 1 0 12:16 ? 00:00:00 /usr/local/bin/redis-server 127.0.0.1:6379
root 10149 10082 0 12:16 pts/7 00:00:00 grep --color=auto redis
[root@iZ2zegbfy ~]#
至此,即配置完成,可通过chkconfig命令查看系统服务运行状态。
chkconfig --list
显示所有运行级系统服务的运行状态信息(on或off)
[root@iZ2zegbfy ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
redis 0:off 1:off 2:on 3:on 4:on 5:on 6:off
(完)