当前位置: 首页 > news >正文

编写Redis开机自启动脚本

1、创建脚本文件

touch /etc/init.d/redis.sh

2、添加启动脚本内容

首先执行

vi /etc/init.d/redis.sh

将以下内容添加到redis.sh文件中:

#!/bin/bash
 #chkconfig: 22345 10 90
 #description: Start and Stop redisREDISPORT=6379
 EXEC=/usr/local/redis/bin/redis-server
 CLIEXEC=/usr/local/redis/bin/redis-cli
 PIDFILE=/var/run/redis_6379.pid
 CONF="/usr/local/redis/etc/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..."
             $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
         sleep 3
         "$0" start
         ;;
     *)
         echo "Please use start or stop or restart as first argument"
         ;;
 esac

3、设置脚本权限

chmod +x /etc/init.d/redis.sh

4、创建启动链接

ln -s /etc/init.d/redis.sh /etc/rc.d/

5、添加到开机启动服务

chkconfig --add redis.sh

6、设置开启自启动

chkconfig redis.sh on

7、启动Redis服务

service redis.sh start

8、验证开启自启动

首先重启电脑,再执行如下命令

ps aux|grep redis

 显示如下界面

说明设置成功

9、创建 redis 用户和组

查看配置文件权限:

ls -l /path/to/redis.conf

确保 Redis 用户(通常为 redis)有读取权限

修复权限:

sudo chown redis:redis /path/to/redis.conf
sudo chmod 644 /path/to/redis.conf

如果系统中没有 redis 用户和组,需要手动创建。

创建用户和组

sudo groupadd redis
sudo useradd -r -g redis -s /bin/false redis

-r:创建系统用户。

-g redis:指定用户的主组为 redis

-s /bin/false:禁止用户登录。

运行以下命令,确认用户和组已创建:

id redis

输出示例:

设置权限

sudo chown -R redis:redis /var/lib/redis

 


文章转载自:
http://analyzable.hfytgp.cn
http://chaldron.hfytgp.cn
http://burble.hfytgp.cn
http://battel.hfytgp.cn
http://ceruse.hfytgp.cn
http://autecologically.hfytgp.cn
http://cardigan.hfytgp.cn
http://chariotee.hfytgp.cn
http://byobu.hfytgp.cn
http://anteroom.hfytgp.cn
http://alae.hfytgp.cn
http://arose.hfytgp.cn
http://brucine.hfytgp.cn
http://bbc.hfytgp.cn
http://buddhism.hfytgp.cn
http://apothem.hfytgp.cn
http://chiaroscurist.hfytgp.cn
http://carbocyclic.hfytgp.cn
http://benison.hfytgp.cn
http://accommodation.hfytgp.cn
http://acropolis.hfytgp.cn
http://camphor.hfytgp.cn
http://bloop.hfytgp.cn
http://alfisol.hfytgp.cn
http://camphoric.hfytgp.cn
http://aga.hfytgp.cn
http://airmobile.hfytgp.cn
http://bakery.hfytgp.cn
http://bootleg.hfytgp.cn
http://antimonial.hfytgp.cn
http://www.dtcms.com/a/40311.html

相关文章:

  • 《CentOS Stream 9 阿里云 yum 源修改:解锁系统更新新速度》
  • Unity百游修炼(3)——Tank_Battle(双人对战)详细制作全流程
  • Vue纷争:DefineProps 的作用是否因为DefineModel而减弱
  • 日语学习-日语知识点小记-构建基础-JLPT-N4N5阶段(12):普通(ふつう)形 :变化方式 :日常朋友家人之间对话
  • 基于SpringBoot + Spring AI + Vue的全栈AI对话系统开发全流程解析
  • 使用uniapp内置组件webview消息传递
  • Zookeeper(67) Zookeeper在HBase中的应用是什么?
  • MySQL进阶04-MVCC实现原理
  • Unity中动态切换光照贴图的方法
  • 京准电钟解读:为何不能用网络上的NTP时间源服务器
  • leetcode 1472. 设计浏览器历史记录 中等
  • 数据存储:一文掌握存储数据到MongoDB详解
  • 【PyTorch][chapter-33][transformer-5] MHA MQA GQA, KV-Cache
  • Sublime Text4安装、汉化
  • WordPress网站502错误全面排查与解决指南
  • prometheus部署和告警设置
  • (十)趣学设计模式 之 外观模式!
  • 【计算机网络】IP协议
  • 飞鱼科技游戏策划岗内推
  • PydanticToolsParser 工具(tool call)把 LLM 生成的文本转成结构化的数据(Pydantic 模型)过程中遇到的坑
  • 第一章 SQL简介
  • python 把数组转字符串的4种方法
  • Buildroot 添加自定义模块-内置文件到文件系统
  • ffmpeg avdevice_register_all 注册设备的作用
  • Git 分支管理
  • Java基础关键_012_包装类
  • Linux的文件与目录管理
  • Redis7——基础篇(八)
  • Visual Studio Code FAQ
  • 零基础学习机器学习分类模型