解决每过一段时间需重启trojan服务才能正常访问的问题
sreio opened this issue · 1 comments
sreio commented
crontab 定时任务
* * * * * sh ~/vpn.sh > /dev/null
shell 脚本内容
#!/bin/bash
# 设置要检查的URL
URL="需要检测的URL"
# 使用curl命令发送HTTP请求并提取状态码
CODE=$(curl --connect-timeout 8 -I -L $URL 2>/dev/null | head -1 | awk '{print $2}')
# 获取当前时间
TIME=$(date '+%Y-%m-%d %H:%M:%S')
# 获取当前日期
DAY=$(date +%Y%m%d)
# 定义日志文件路径
LOGFILE="/var/log/trojanlog/check-trojan${DAY}.log"
# 如果日志目录不存在,则创建它
[ ! -d "/var/log/trojanlog" ] && mkdir -p /var/log/trojanlog
# 检查HTTP状态码是否为200
if [ "$CODE" -eq 200 ]; then
# 如果状态码为200,则将当前时间和"ok"追加到日志文件中并退出脚本 这个可不写,只保留 `exit`即可
echo "${TIME} ok" >> ${LOGFILE}
exit
else
# 如果状态码不是200,则重新启动trojan服务并记录重启信息到日志文件中
systemctl restart trojan
echo "${TIME} 已重启trojan服务" >> ${LOGFILE}
fi
# 删除7天前的日志文件
find /var/log/trojanlog/ -name "*.log" -type f -mtime +7 -delete
Tip:
需要检测的url最好设置成网站的静态资源文件,一旦服务挂掉,静态资源不可访问,此时我们可以进行重启操作
每个网站打包后的静态资源文件都不相同,请自行copy,如下:
![image](https://private-user-images.githubusercontent.com/37869069/250713336-d91d70d5-39c8-4527-a18a-b0297ddfc147.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MDQyMTk0NDAsIm5iZiI6MTcwNDIxOTE0MCwicGF0aCI6Ii8zNzg2OTA2OS8yNTA3MTMzMzYtZDkxZDcwZDUtMzljOC00NTI3LWExOGEtYjAyOTdkZGZjMTQ3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDAxMDIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwMTAyVDE4MTIyMFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTc5MjhhZTgwNTk4YzdiOTY0Zjc4NTg0OWI4ZDhjNTk5Nzk2NDgyNWNkMzRmOWVjNWJmMDNkNTBhOThjZjA5OTImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.KnOMt2vk5jhXeeahA_BZ3xFA-TYsUqSa5cm2G2EanJ4)
sreio commented
resolved