网站的redis服务经常会因为缓存问题而停止,显示错误代码:
Error establishing a Redis connection
我们可以在宝塔面板设置shell命令。
#!/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="http://example.com" # 替换为你要检测的网站 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分钟检测一次,保证网站能正常访问,当然也可以设置其他时间。