Redis是置开(kai)一款开源的内存数(shu)据结构存储系(xi)统,它可以用作数据库、机自缓存和消息代理,启动在许多应(ying)用场景中,置开Redis都(dou)发挥着重要的机自作用,有(you)时候我们希望在系统(tong)启动时自动运行Redis,启动以便及(ji)时处理(li)一些初始化任务或者提供实时的数据(ju)服务,本文将(jiang)介绍如(ru)何设置Redis开机自启动。置开
在设置Redis开机自启动之前,我们需要确保Redis已经正确(que)安装并配置好,置开(kai)可以通过(guo)以下命令检查Redis是机自否已经安装(zhuang):


redis-cli --version
如果显示出Redis的(de)版本信息,说明Redis已经(jing)安装成功,启动如果没有安装成功,置开请参考Redis官方文档进行安装:

为了让Redis在开机时自(zi)动启动,启动(dong)我们需要创建一个配置文件,配置文件通常位于`/etc/redis/redis.conf`,如果该文件不存在,可(ke)以手动创建一个,在配置文件中,我们需要设置以下几个参(can)数(shu):
1. `daemonize no`:这(zhe)个参数表示让Redis以非守护进程方式运行,这(zhe)样在后台运(yun)行时不会影响到系统的正常运行。
2. `pidfile /var/run/redis_6379.pid`:这个参数表示设置Redis的进程ID文(wen)件路径,当Redis作(zuo)为守护进程运行时,需要使用这个PID文件来管理进程。
3. `port 6379`:这个参数表示设置Redis监听的端口号,默认(ren)为6379,可以根据实际(ji)需求修改端口号。
4. `bind 127.0.0.1`:这(zhe)个参数表示设置Redis只能(neng)监听本地回环地址,不能接受外部连接,如果需要让其他设备访问Redis,可以(yi)将此参数注释掉或(huo)者修改为(wei)相应的(de)IP地址。
5. `loglevel notice`:这个参数表示设置Redis的日志级别为notice,这样只会输出警告和错误信息,方便排查问题。
为了让Redis在开机时自(zi)动启动,我们需要创建一个开机自(zi)启动脚本,在`/etc/init.d`目录下创建一个名(ming)为(wei)`redis_6379`的脚本文(wen)件:
sudo touch /etc/init.d/redis_6379sudo chmod +x /etc/init.d/redis_6379
接(jie)下来,编辑`redis_6379`脚本文件,添加以下内容:
#!/bin/sh### BEGIN INIT INFO# Provides: redis_6379# required-Start: $remote_fs $syslog# required=""-Stop: $remote_fs $syslog# Default-Start: 2 3 4 5# Default-Stop: 0 1 6# Short-Description: Start Redis on boot# Description: Enable service provided by redis.service.### END INIT INFOcase "$1" in start) echo "Starting Redis ..." /etc/init.d/redis-server start >> /var/log/redis_start.log 2>&1 & echo $! > /var/run/redis_6379.pid ;; stop) echo "Stopping Redis ..." /etc/init.d/redis-server stop >> /var/log/redis_stop.log 2>&1 & kill $(cat /var/run/redis_6379.pid) && echo $? > /var/run/redundant_shutdown_status.txt || echo "failed to stop Redis" rm -f /var/run/redis_6379.pid ;; *) echo "Usage: $0 start|stop" >&2 exit 1 ;;esacexit 0保存文件后,给脚本文件(jian)添加可执行权限:
sudo chmod +x /etc/init.d/redis_6379为了让系(xi)统在启动时自动加载我们的开机自启动脚本,我们需要创建(jian)一个链接,在`/etc/rc.local`文件(jian)中添加以下内容:
#!/bin/sh -e### BEGIN INIT INFO# Provides: rc-local update script (required="required" for systemctl)# required="required"-Start: $remote_fs $syslog $network $named $time $syslogd $tmpfs $tcpdump $udpdump icmp $rtcwake $crond $syslogd $maildrop $messagebus *# required="required"-Stop: $remote_fs $syslog $network $named $time $syslogd $tmpfs $tcpdump $udpdump icmp $rtcwake $crond $syslogd $maildrop $messagebus *# Default-Start: 2 3 4 5# Default-Stop: 0 1 6# Short-Description: Run custom scripts at startup before normal process startup.# Description: This script is executed at the end of each multiuser runlevel.### END INIT INFOif [ "$RUNLEVEL" == "0" ]; then # After this level, all processes not listed below will be stopped.${ PATH=~^(?::/usr/localbin:)${ PATH}} [ "$TERM" != "dumb" ] && exec sudo su --login --command "${ PATH=~^(?::/usr/localbin:)${ PATH}} date; $HOME/apps/redis_6379 start" &>> "$HOME"/apps/logs/redis_startup.log & disown # Add any command specific options above this line if needed.${ PATH=~^(?::/usr/localbin:)${ PATH}} [ "$TERM" = "dumb" ] && exec sh <<EOF # Otherwise, do nothing and continue with normal startup of shell or other process.${ PATH=~^(?::/usr/localbin:)${ PATH}} while true; do sleep 10; doneEOF &>> "$HOME"/apps/logs/redis_startup.log & disown # This ensures that the container does not get locked (in case of CPU or memory constraints).${ PATH=~^(?::/usr/localbin:)${ PATH}} fi # End script execution if it runs successfully (success means that the last command executed returned a zero exit code).${ PATH=~^(?::/usr/localbin:)${ PATH}} exit 0 # Exit status of the script (0 indicates success).${ PATH=~^(?::/usr/localbin:)${ PATH}}
(作者:网站优化)