使用shell脚本设置自动重启redis

网站的redis服务经常会因为缓存问题而停止,显示错误代码:

代码语言:javascript代码运行次数:0运行复制
Error establishing a Redis connection

我们可以在宝塔面板设置shell命令。

代码语言:javascript代码运行次数:0运行复制
#!/bin/bash

# 检测 Redis 是否在运行
redis_status=$(systemctl is-active redis.service)

if [ "$redis_status" != "active" ]; then
    echo "Redis is not running. Restarting Redis..."
    systemctl start redis.service
    echo "Redis restarted."
fi

# 检测网站是否可访问
website_url=";  # 替换为你要检测的网站 URL
http_status=$(curl -s -o /dev/null -w "%{http_code}" $website_url)

if [ "$http_status" != "200" ]; then
    echo "Website is not accessible. Restarting Redis..."
    systemctl restart redis.service
    echo "Redis restarted."
fi

设置时间为每5分钟检测一次,保证网站能正常访问,当然也可以设置其他时间。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2023-07-19,如有侵权请联系 cloudcommunity@tencent 删除redis服务脚本网站shell