首页 » 运维教程 » 正文

怎么让redis开机自启「设置redis开机自启」

眉心 2024-09-20 运维教程 94 views 0

扫一扫用手机浏览

文章目录 [+]

让Redis开机自启

Redis是一个高性能的键值存储系统,广泛应用于各种应用场景中,为了确保Redis在服务器重启后能够自动运行,我们需要将其配置为开机自启,下面将详细介绍如何实现Redis的开机自启。

1. 安装Redis

我们需要在服务器上安装Redis,可以通过以下命令来安装:

sudo apt-get update
sudo apt-get install redis-server

2. 创建Redis配置文件

默认情况下,Redis会使用`/etc/redis/redis.conf`作为配置文件,我们可以编辑该文件来自定义Redis的配置,使用文本编辑器打开该文件:

sudo nano /etc/redis/redis.conf

3. 配置开机自启

在配置文件中找到`daemonize no`这一行,将其修改为`daemonize yes`,表示以守护进程方式运行Redis,保存并关闭文件。

4. 创建启动脚本

为了实现开机自启,我们需要创建一个启动脚本并将其添加到系统的启动项中,创建一个名为`redis_start.sh`的脚本文件:

sudo nano /etc/init.d/redis_start.sh

在该文件中添加以下内容:

#!/bin/bash
# chkconfig: 2345 80 90
# description: Redis is an in-memory data structure store, used as the distributed, in-memory key-value database, cache and message broker, with optional durability.
# processname: redis_6379
# pidfile: /var/run/redis_6379.pid
# config: /etc/redis/redis.conf
 BEGIN INIT INFO
# Provides:          redis_6379
# Required-Start:    $remote_fs $syslog $named $network $time $portmap $xinetd $limitfs $setgid
# Required-Stop:     $remote_fs $syslog $named $network $time $portmap $xinetd $limitfs $setgid
# Should-Start:      $network $time $syslog
# Should-Stop:       $network $time $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the redis server daemonized mode
# Description:       starts the redis server daemonized mode providing fault tolerance, access through network, and data recovery. See http://redis.io for details.
 END INIT INFO
REDISPORT=6379
EXEC=/usr/local/bin/redis-server /etc/redis/redis.conf --port ${REDISPORT} --daemonize yes >> /var/log/redis_${REDISPORT}.log 2>&1 &
PIDFILE=/var/run/redis_${REDISPORT}.pid
case "$1" in
 start)
    echo "Starting Redis..."
    if [ -f ${PIDFILE} ] && kill -0 `cat ${PIDFILE}`; then
        echo "Redis is already running." >> /var/log/redis_${REDISPORT}.log 2>&1
    else
        echo "Starting Redis..." >> /var/log/redis_${REDISPORT}.log 2>&1 & ${EXEC}
        sleep 10s # wait for the server to start completely before adding it to the init process list. Otherwise, we may get a "timeout" error when trying to stop the server later on.
        echo "Redis started." >> /var/log/redis_${REDISPORT}.log 2>&1 & touch ${PIDFILE} # create the PID file so that we can signal the server to stop later on.
    fi;;
 stop)
    if [ -f ${PIDFILE} ] && kill -0 `cat ${PIDFILE}`; then
        echo "Stopping Redis..." >> /var/log/redis_${REDISPORT}.log 2>&1 & kill -TERM `cat ${PIDFILE}` && wait `cat ${PIDFILE}` && echo "Redis stopped." >> /var/log/redis_${REDISPORT}.log 2>&1 & rm -f ${PIDFILE} # gracefully stop the server first by sending it SIGTERM and then wait for it to exit before removing the PID file and logging the event. else echo "Cannot find the PID file or the server is not running." >> /var/log/redis_${REDISPORT}.log 2>&1; fi;;
 restart)
    if [ -f ${PIDFILE} ] && kill -0 `cat ${PIDFILE}`; then echo "Restarting Redis..." >> /var/log/redis_${REDISPORT}.log 2>&1 & kill -TERM `cat ${PIDFILE}` && sleep 10s && ${EXEC} && echo "Redis restarted." >> /var/log/redis_${REDISPORT}.log 2>&1 & else echo "Cannot find the PID file or the server is not running." >> /var/log/redis_${REDISPORT}.log 2>&1; fi;; status) if [ -f ${PIDFILE} ] && kill -0 `cat ${PIDFILE}`; then echo "Redis is running." >> /var/log/redis_${REDISPORT}.log 2>&1 & else echo "Redis is not running." >> /var/log/redis_${REDISPORT}.log 2>&1; fi;; *) echo "Usage: service {start|stop|restart|status}" >> /var/log/redis_${REDISPORT}.log 2>&1;;
esac
exit 0

保存并关闭文件,给脚本文件添加执行权限:

sudo chmod +x /etc/init.d/redis_start.sh

5.

标签:

相关推荐

linux所有的定时命令?

在Linux系统中,定时任务是一种非常实用的功能,它可以帮助我们在特定的时间执行指定的命令,本文将详细介绍Linux中所有的定时命...

运维教程 2024-09-20 阅读69 评论0

redis怎么查看操作日志文件

在Redis中,你可以通过修改配置文件来查看操作日志文件,默认情况下,Redis的操作日志文件位于Redis配置文件中指定的路径下...

运维教程 2024-09-20 阅读96 评论0

怎么解决服务器的503问题和问题

一、什么是503错误503错误,又称为“Service Unavailable”,是一种HTTP状态码,表示服务器暂时无法处理请求...

技术 2024-09-20 阅读84 评论0

js获取当前日期时间/年份/月份

在Web开发中,我们经常需要获取当前的日期和时间,JavaScript提供了一些内置的API,可以帮助我们轻松地完成这项任务,本文...

运维cms 2024-09-20 阅读89 评论0

如何查看redis集群日志信息

一、Redis集群日志简介Redis集群是Redis官方提供的一种分布式解决方案,它可以将数据分片存储在多个节点上,从而提高数据的...

运维教程 2024-09-20 阅读53 评论0

redis缓存机制的好处有哪些

Redis缓存机制的好处:1. 提高数据读取速度:Redis作为内存数据库,数据存储在内存中,读取速度比磁盘快得多,大大提高了数据...

运维教程 2024-09-20 阅读35 评论0