From bfe0af9ea2cd631d3374613aa9d4c759e9f2149e Mon Sep 17 00:00:00 2001 From: Aaron Chen Date: Thu, 6 Jul 2023 09:56:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84auth=5Fbuild.sh=E5=92=8Cauto?= =?UTF-8?q?=5Fcleaner.sh=EF=BC=8C=E6=8F=90=E5=8D=87=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=89=93=E5=8C=85=E8=BF=87=E7=A8=8B=E7=9A=84=E7=A8=B3=E5=AE=9A?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- README.md | 14 ++++++++++ auto_build.sh | 74 +++++++++++++++++++++++++++++++++++++++++++++++-- auto_cleaner.sh | 37 +++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 auto_cleaner.sh diff --git a/.gitignore b/.gitignore index 592869f..2da60ee 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ dist .user.ini .env *.log -*.log.* \ No newline at end of file +*.log.* +*.lock \ No newline at end of file diff --git a/README.md b/README.md index e8bdac9..a6c3b2d 100644 --- a/README.md +++ b/README.md @@ -60,3 +60,17 @@ php start.php start -d ``` > 需要开放程序所在服务器的8787端口,或者做一个反向代理到8787端口的http站点。 + +### 避免因自动打包进程意外终止而导致永远无法自动打包 + +```sh +crontab -e +``` + +添加下面一行 + +```crontab +*/10 * * * * /path/to/auto_cleaner.sh /path/to/ >/dev/null 2>&1 +``` + +其中`/path/to/`要替换成实际路径。 diff --git a/auto_build.sh b/auto_build.sh index af06253..a3a53cb 100644 --- a/auto_build.sh +++ b/auto_build.sh @@ -1,7 +1,75 @@ #!/bin/bash -git pull origin master +log_file="auto_build.log" +lock_file="auto_build.lock" +timeout=600 # 超时时间,单位为秒 -yarn install +# 记录日志函数 +log() { + echo "[$(date '+%Y-%m-%d %H:%M:%S')] [$$] $1" >> "$log_file" +} -yarn build \ No newline at end of file +# 记录开始执行日志 +log "start" + +# 检查是否有其他进程正在执行 +while [ -f "$lock_file" ]; do + another_pid=$(cat "$lock_file") + log "另一个进程[$another_pid]正在执行,等待中……" + sleep 5 + timeout=$((timeout - 1)) + if [ "$timeout" -le 0 ]; then + log "等待超时,退出" + exit 1 + fi +done + +# 创建锁文件 +echo $$ > "$lock_file" + +# 执行git pull +execute() { + log "$1" + result=$(eval "$1" 2>&1) + log "$result" +} + +# 执行git pull +execute "git pull" + +# 检查git pull执行结果 +if [ $? -eq 0 ]; then + # git pull执行成功,执行yarn install + execute "yarn install" + + # 检查yarn install执行结果 + if [ $? -eq 0 ]; then + # yarn install执行成功,执行yarn build + execute "yarn build" + + # 检查yarn build执行结果 + if [ $? -ne 0 ]; then + # yarn build执行失败,异常退出 + log "yarn build执行失败" + rm "$lock_file" + exit 1 + fi + else + # yarn install执行失败,异常退出 + log "yarn install执行失败" + rm "$lock_file" + exit 1 + fi +else + # git pull执行失败,异常退出 + log "git pull执行失败" + rm "$lock_file" + exit 1 +fi + +# 记录完成执行日志 +log "done" + +# 删除锁文件并正常退出 +rm "$lock_file" +exit 0 diff --git a/auto_cleaner.sh b/auto_cleaner.sh new file mode 100644 index 0000000..a753bfc --- /dev/null +++ b/auto_cleaner.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +log_file="auto_cleaner.log" +lock_dir="${1:-./}" +lock_file="${lock_dir}auto_build.lock" +timeout=600 # 超时时间,单位为秒 + +# 记录日志函数 +log() { + echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$log_file" +} + +# 拼接锁文件路径 +lock_file="${lock_dir}auto_build.lock" + +# 判断锁文件是否存在 +if [ -f "$lock_file" ]; then + # 获取锁文件中的进程ID + pid=$(cat "$lock_file") + + # 判断系统中是否有该进程 + if ps -p "$pid" >/dev/null; then + # 判断进程名称是否包含auto_build.sh + process_name=$(ps -o comm= -p "$pid") + if [[ "$process_name" == *"auto_build.sh"* ]]; then + log "进程[$pid]异常,释放$lock_file" + rm "$lock_file" + else + log "进程[$pid]验证正常" + fi + else + log "进程[$pid]异常,释放$lock_file" + rm "$lock_file" + fi +else + log "未发现$lock_file" +fi