From c77eb3d28d9153edd6f26e3b4b206d6e9ce551f7 Mon Sep 17 00:00:00 2001 From: hanhh <18330117883@163.com> Date: Tue, 29 Jun 2021 00:05:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0ql=20check=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E7=9B=91=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/docker-entrypoint.sh | 8 +-- shell/check.sh | 110 ++++++++++++++++++++++++++++++++++++ shell/reset.sh | 30 ---------- shell/share.sh | 1 - shell/task.sh | 8 +-- shell/update.sh | 14 ++--- 6 files changed, 124 insertions(+), 47 deletions(-) create mode 100644 shell/check.sh delete mode 100644 shell/reset.sh diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index c7c73e49..8f223ef8 100755 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -20,17 +20,17 @@ echo -e "nginx启动成功...\n" echo -e "======================4. 启动控制面板========================\n" if [[ $(pm2 info panel 2>/dev/null) ]]; then - pm2 reload panel + pm2 reload panel --source-map-support --time else - pm2 start $dir_root/build/app.js -n panel + pm2 start $dir_root/build/app.js -n panel --source-map-support --time fi echo -e "控制面板启动成功...\n" echo -e "======================5. 启动定时任务========================\n" if [[ $(pm2 info schedule 2>/dev/null) ]]; then - pm2 reload schedule + pm2 reload schedule --source-map-support --time else - pm2 start $dir_root/build/schedule.js -n schedule + pm2 start $dir_root/build/schedule.js -n schedule --source-map-support --time fi echo -e "定时任务启动成功...\n" diff --git a/shell/check.sh b/shell/check.sh new file mode 100644 index 00000000..0847c902 --- /dev/null +++ b/shell/check.sh @@ -0,0 +1,110 @@ +#!/usr/bin/env bash + +dir_shell=/ql/shell +. $dir_shell/share.sh +. $dir_shell/api.sh +get_token + +panelLogPath="/root/.pm2/logs/panel-error.log" + +reset_env() { + echo -e "---> 1. 开始检测配置文件\n" + fix_config + echo -e "---> 配置文件检测完成\n" + + echo -e "---> 2. 开始安装青龙依赖\n" + npm_install_2 $dir_root + echo -e "---> 青龙依赖安装完成\n" + + echo -e "---> 3. 开始安装脚本依赖\n" + cp -f $dir_sample/package.json $dir_scripts/package.json + npm_install_2 $dir_scripts + echo -e "---> 脚本依赖安装完成\n" + + echo -e "---> 4. 复制通知文件\n" + echo -e "---> 复制一份 $file_notify_py_sample 为 $file_notify_py\n" + cp -fv $file_notify_py_sample $file_notify_py + echo + echo -e "---> 复制一份 $file_notify_js_sample 为 $file_notify_js\n" + cp -fv $file_notify_js_sample $file_notify_js + echo -e "---> 通知文件复制完成\n" +} + +reload_pm2() { + pm2 l >/dev/null 2>&1 + + if [[ $(pm2 info panel 2>/dev/null) ]]; then + pm2 reload panel --source-map-support --time >/dev/null 2>&1 + else + pm2 start $dir_root/build/app.js -n panel --source-map-support --time >/dev/null 2>&1 + fi + + if [[ $(pm2 info schedule 2>/dev/null) ]]; then + pm2 reload schedule --source-map-support --time >/dev/null 2>&1 + else + pm2 start $dir_root/build/schedule.js -n schedule --source-map-support --time >/dev/null 2>&1 + fi +} + +pm2_log() { + echo -e "---> pm2日志" + local panelOut="/root/.pm2/logs/panel-out.log" + local panelError="/root/.pm2/logs/panel-error.log" + tail -n 10 "$panelOut" + tail -n 10 "$panelError" +} + +check_nginx() { + local nginxPid=$(ps -ef | grep nginx | grep -v grep) + echo -e "---> nginx服务\n$nginxPid" + if [[ $nginxPid ]]; then + echo -e "\n=====> nginx服务正常\n" + else + echo -e "\n=====> nginx服务异常,重新启动nginx\n" + nginx -c /etc/nginx/nginx.conf + fi +} + +check_ql() { + local api=$(curl -s "http://localhost:5700") + echo -e "\n=====> 检测面板\n\n$api\n" + if [[ $api =~ "
" ]]; then + echo -e "=====> 面板服务启动正常\n" + check_nginx + else + echo -e "=====> 面板服务异常,重置基础环境\n" + reset_env + fi +} + +check_pm2() { + pm2_log + local currentTimeStamp=$(date +%s) + local api=$( + curl -s "http://localhost:5600/api/user?t=$currentTimeStamp" \ + -H 'Accept: */*' \ + -H "Authorization: Bearer $token" \ + -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36' \ + -H 'Referer: http://localhost:5700/crontab' \ + -H 'Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7' \ + --compressed + ) + echo -e "\n=====> 检测后台\n\n$api\n" + if [[ $api =~ "{\"code\"" ]]; then + echo -e "=====> 后台服务启动正常\n" + else + echo -e "=====> 后台服务异常,重启后台\n" + reload_pm2 + fi +} + +main() { + echo -e "=====> 开始检测" + check_ql + check_pm2 + echo -e "\n=====> 检测结束\n" +} + +main + +exit diff --git a/shell/reset.sh b/shell/reset.sh deleted file mode 100644 index 376c7225..00000000 --- a/shell/reset.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env bash - -# 导入通用变量与函数 -dir_shell=/ql/shell -. $dir_shell/share.sh - -echo -e "1. 开始检测配置文件\n" -fix_config -echo -e "配置文件检测完成\n" - -echo -e "2. 开始安装青龙依赖\n" -npm_install_2 $dir_root -echo -e "青龙依赖安装完成\n" - -echo -e "3. 开始安装脚本依赖\n" -cp -f $dir_sample/package.json $dir_scripts/package.json -npm_install_2 $dir_scripts -echo -e "脚本依赖安装完成\n" - -echo -e "4. 复制通知文件\n" -echo -e "复制一份 $file_notify_py_sample 为 $file_notify_py\n" -cp -fv $file_notify_py_sample $file_notify_py -echo - -echo -e "复制一份 $file_notify_js_sample 为 $file_notify_js\n" -cp -fv $file_notify_js_sample $file_notify_js - -echo -e "通知文件复制完成\n" - -exit 0 diff --git a/shell/share.sh b/shell/share.sh index 23b7482f..7a9e51cd 100755 --- a/shell/share.sh +++ b/shell/share.sh @@ -213,7 +213,6 @@ npm_install_sub() { elif ! type pnpm >/dev/null 2>&1; then npm install --production --no-save --registry=https://registry.npm.taobao.org || npm install --production --no-save else - echo -e "检测到本机安装了 pnpm,使用 pnpm 替代 ...\n" pnpm install --prod fi } diff --git a/shell/task.sh b/shell/task.sh index b16e778a..70d44bdd 100755 --- a/shell/task.sh +++ b/shell/task.sh @@ -90,14 +90,14 @@ run_normal() { local id=$(cat $list_crontab_user | grep -E "$cmd_task $p1$" | perl -pe "s|.*ID=(.*) $cmd_task $p1$|\1|" | xargs | sed 's/ /","/g') local begin_time=$(date '+%Y-%m-%d %H:%M:%S') - echo -e "## 开始执行... $begin_time\n" >> $log_path - update_cron_status "\"$id\"" "0" + echo -e "## 开始执行... $begin_time\n" | tee -a $log_path + [[ $id ]] && update_cron_status "\"$id\"" "0" timeout $command_timeout_time $which_program $p1 2>&1 | tee -a $log_path . $file_task_after - update_cron_status "\"$id\"" "1" + [[ $id ]] && update_cron_status "\"$id\"" "1" local end_time=$(date '+%Y-%m-%d %H:%M:%S') local diff_time=$(($(date +%s -d "$end_time") - $(date +%s -d "$begin_time"))) - echo -e "\n## 执行结束... $end_time 耗时 $diff_time 秒" >> $log_path + echo -e "\n## 执行结束... $end_time 耗时 $diff_time 秒" | tee -a $log_path } ## 并发执行时,设定的 RandomDelay 不会生效,即所有任务立即执行 diff --git a/shell/update.sh b/shell/update.sh index d6a7f779..073ca4f4 100755 --- a/shell/update.sh +++ b/shell/update.sh @@ -242,8 +242,6 @@ update_qinglong() { echo fi - fix_config - [ -f $dir_root/package.json ] && ql_depend_old=$(cat $dir_root/package.json) reset_romote_url ${dir_root} "${github_proxy_url}https://github.com/whyour/qinglong.git" git_pull_scripts $dir_root @@ -295,15 +293,15 @@ reload_pm2() { pm2 l >/dev/null 2>&1 if [[ $(pm2 info panel 2>/dev/null) ]]; then - pm2 reload panel >/dev/null 2>&1 + pm2 reload panel --source-map-support --time >/dev/null 2>&1 else - pm2 start $dir_root/build/app.js -n panel >/dev/null 2>&1 + pm2 start $dir_root/build/app.js -n panel --source-map-support --time >/dev/null 2>&1 fi if [[ $(pm2 info schedule 2>/dev/null) ]]; then - pm2 reload schedule >/dev/null 2>&1 + pm2 reload schedule --source-map-support --time >/dev/null 2>&1 else - pm2 start $dir_root/build/schedule.js -n schedule >/dev/null 2>&1 + pm2 start $dir_root/build/schedule.js -n schedule --source-map-support --time >/dev/null 2>&1 fi } @@ -436,9 +434,9 @@ main() { echo -e "## 开始执行... $begin_time\n" >> $log_path . $dir_shell/bot.sh | tee -a $log_path ;; - reset) + check) echo -e "## 开始执行... $begin_time\n" >> $log_path - . $dir_shell/reset.sh | tee -a $log_path + . $dir_shell/check.sh | tee -a $log_path ;; *) echo -e "命令输入错误...\n"