创建auto_install.sh脚本以便自动添加执行auto_cleaner.sh的定时任务

This commit is contained in:
wandoubaba 2023-07-06 10:25:18 +08:00
parent bfe0af9ea2
commit fcccdcdbdf
2 changed files with 40 additions and 9 deletions

View File

@ -63,14 +63,8 @@ php start.php start -d
### 避免因自动打包进程意外终止而导致永远无法自动打包
可以通过执行`auto_install.sh`脚本创建一个系统定时任务,用来定时清理由`auto_build.sh`脚本错误执行而产生的“僵尸锁”。
```sh
crontab -e
sh auto_install.sh
```
添加下面一行
```crontab
*/10 * * * * /path/to/auto_cleaner.sh /path/to/ >/dev/null 2>&1
```
其中`/path/to/`要替换成实际路径。

37
auto_install.sh Normal file
View File

@ -0,0 +1,37 @@
#!/bin/bash
lock_file="auto_install.lock"
cleaner_script="auto_cleaner.sh"
cron_command="sh $(pwd)/$cleaner_script $(pwd)/"
# 检查是否已安装
if [ -f "$lock_file" ]; then
echo "auto_cleaner.sh任务已安装如需重新安装请先删除$lock_file文件"
exit 1
fi
# 获取当前目录的绝对路径
current_dir=$(pwd)/
# 检查auto_cleaner.sh文件是否存在
if [ ! -f "$current_dir$cleaner_script" ]; then
echo "未找到$current_dir$cleaner_script文件,退出本次安装"
exit 1
fi
# 检查是否已存在定时任务
existing_cron=$(crontab -l | grep -F "$cron_command")
if [ -n "$existing_cron" ]; then
echo "auto_cleaner.sh任务已安装退出本次安装"
exit 1
fi
# 创建安装标记文件
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
echo "安装时间: $timestamp" > "$lock_file"
# 创建定时任务
(crontab -l 2>/dev/null; echo "*/10 * * * * $cron_command") | crontab -
echo "auto_cleaner.sh任务安装完毕"
exit 0