qinglong/shell/task.sh
whyour 5b3687f7b6 全新青龙2.0 (#65)
* 重构shell (#17)

* 更新正则

* 更新update命令

* 移除测试代码

* 重构删除日志命令

* 更新entrypoint

* 更新dockerfile

* 完善shell调用

* 修复share shell引用

* 修复entrypoint

* 修复share shell

* 修复share.sh

* 修改依赖重装逻辑

* 更新docker entrypoint

* curl 使用静默模式

* 更新ql raw

* 修复添加单个任务

* 修复shell语法

* 添加定时任务进程

* 更新默认定时任务

* 更新定时任务重启schedule

* 更新青龙重启逻辑

* 修复定时任务列表创建

* 修复schedule进程

* 修复entrypoint

* 更新task命令

* pm2 restart替换成reload

* 修复task命令参数引入

* 完善ql repo命令

* 修复update.sh

* 更新ql repo命令

* ql repo添加登录验证,修复package.json示例

* 修复定时任务命令补全

* 修改默认cron端口

* 修复cron日志弹框异常

* 修改cron新建label

* 修复ql repo命令

* 修复cron格式验证

* 修改日志目录格式

* 修改青龙remote url

* 修复添加定时cron匹配

* 添加定时任务超时时间设置

* 暂时移除timeout命令

* 恢复定时任务timeout

* 修复cookie.sh引用

* 修复shell变量自加

* 修复ck更新状态同步

* 增加tg bot测试,修改增删任务通知

* 修复shell函数返回值

* 修改添加任务日志打印

* 修改entrypoint日志

* 修复api日志打印

* 修改api日志打印

* 定时任务支持批量启用禁用删除运行

* 修改cron管理操作按钮响应样式

* 更新bot启动脚本

* 更新bot启动脚本

* 增加timeout默认值,修改session管理逻辑

* 更新config示例和通知日志

* 更新bot.sh

* 更新启动bot命令

* 更新启动bot命令

* 修复task运行参数合并

* 增加停止定时任务功能

* 修复停止定时任务api

* 更新停止定时任务日志

* 更新停止任务日志

* 修复删除cron api

* 更新删除cron通知文本

* 更新命令提示

* 更新bot启动脚本
2021-05-10 20:47:23 +08:00

180 lines
5.5 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
## 导入通用变量与函数
dir_shell=/ql/shell
. $dir_shell/share.sh
## 组合Cookie和互助码子程序$1要组合的内容
combine_sub() {
local what_combine=$1
local combined_all=""
local tmp1 tmp2
for ((i = 1; i <= $user_sum; i++)); do
local tmp1=$what_combine$i
local tmp2=${!tmp1}
combined_all="$combined_all&$tmp2"
done
echo $combined_all | perl -pe "{s|^&||; s|^@+||; s|&@|&|g; s|@+&|&|g; s|@+|@|g; s|@+$||}"
}
## 正常依次运行时组合所有账号的Cookie与互助码
combine_all() {
for ((i = 0; i < ${#env_name[*]}; i++)); do
export ${env_name[i]}=$(combine_sub ${var_name[i]})
done
}
## 并发运行时直接申明每个账号的Cookie与互助码$1用户Cookie编号
combine_one() {
local user_num=$1
for ((i = 0; i < ${#env_name[*]}; i++)); do
local tmp=${var_name[i]}$user_num
export ${env_name[i]}=${!tmp}
done
}
## 选择python3还是node
define_program() {
local p1=$1
if [[ $p1 == *.js ]]; then
which_program="node"
elif [[ $p1 == *.py ]]; then
which_program="python3"
else
which_program=""
fi
}
random_delay() {
local random_delay_max=$RandomDelay
if [[ $random_delay_max ]] && [[ $random_delay_max -gt 0 ]]; then
local current_min=$(date "+%-M")
if [[ $current_min -gt 2 && $current_min -lt 30 ]] || [[ $current_min -gt 31 && $current_min -lt 59 ]]; then
delay_second=$(($(gen_random_num $random_delay_max) + 1))
echo -e "\n命令未添加 \"now\",随机延迟 $delay_second 秒后再执行任务,如需立即终止,请按 CTRL+C...\n"
sleep $delay_second
fi
fi
}
## scripts目录下所有可运行脚本数组
gen_array_scripts() {
local dir_current=$(pwd)
local i="-1"
cd $dir_scripts
for file in $(ls); do
if [ -f $file ] && [[ $file == *.js && $file != sendNotify.js ]]; then
let i++
array_scripts[i]=$(echo "$file" | perl -pe "s|$dir_scripts/||g")
array_scripts_name[i]=$(grep "new Env" $file | awk -F "'|\"" '{print $2}' | head -1)
[[ -z ${array_scripts_name[i]} ]] && array_scripts_name[i]="<未识别出活动名称>"
fi
done
cd $dir_current
}
## 使用说明
usage() {
define_cmd
gen_array_scripts
echo -e "task命令运行本程序自动添加进crontab的脚本需要输入脚本的绝对路径或去掉 “$dir_scripts/” 目录后的相对路径(定时任务中请写作相对路径),用法为:"
echo -e "1.$cmd_task <file_name> # 依次执行如果设置了随机延迟并且当时时间不在0-2、30-31、59分内将随机延迟一定秒数"
echo -e "2.$cmd_task <file_name> now # 依次执行,无论是否设置了随机延迟,均立即运行,前台会输出日志,同时记录在日志文件中"
echo -e "3.$cmd_task <file_name> conc # 并发执行,无论是否设置了随机延迟,均立即运行,前台不产生日志,直接记录在日志文件中"
if [[ ${#array_scripts[*]} -gt 0 ]]; then
echo -e "\n当前有以下脚本可以运行:"
for ((i = 0; i < ${#array_scripts[*]}; i++)); do
echo -e "$(($i + 1)). ${array_scripts_name[i]}${array_scripts[i]}"
done
else
echo -e "\n暂无脚本可以执行"
fi
}
## run nohup$1文件名不含路径带后缀
run_nohup() {
local file_name=$1
nohup node $file_name &>$log_path &
}
## 正常运行单个脚本,$1传入参数
run_normal() {
local p1=$1
cd $dir_scripts
define_program "$p1"
if [[ $p1 == *.js ]]; then
if [[ $AutoHelpOther == true ]] && [[ $(ls $dir_code) ]]; then
local latest_log=$(ls -r $dir_code | head -1)
. $dir_code/$latest_log
fi
if [[ $# -eq 1 ]]; then
random_delay
fi
fi
combine_all
log_time=$(date "+%Y-%m-%d-%H-%M-%S")
log_dir="$dir_log/${p1%%.*}"
log_path="$log_dir/$log_time.log"
make_dir "$log_dir"
timeout $command_timeout_time $which_program $p1 2>&1 | tee $log_path
}
## 并发执行因为是并发所以日志只能直接记录在日志文件中日志文件以Cookie编号结尾前台执行并发跑时不会输出日志
## 并发执行时,设定的 RandomDelay 不会生效,即所有任务立即执行
run_concurrent() {
local p1=$1
cd $dir_scripts
define_program "$p1"
log_dir="$dir_log/${p1%%.*}"
make_dir $log_dir
log_time=$(date "+%Y-%m-%d-%H-%M-%S.%N")
echo -e "\n各账号间已经在后台开始并发执行前台不输入日志日志直接写入文件中。\n"
for ((user_num = 1; user_num <= $user_sum; user_num++)); do
combine_one $user_num
log_path="$log_dir/${log_time}_${user_num}.log"
timeout $command_timeout_time $which_program $p1 &>$log_path &
done
}
## 运行其他命令
run_else() {
local log_time=$(date "+%Y-%m-%d-%H-%M-%S")
local log_dir="$dir_log/$1"
local log_path="$log_dir/$log_time.log"
make_dir "$log_dir"
timeout $command_timeout_time "$@" 2>&1 | tee $log_path
}
## 命令检测
main() {
case $# in
0)
echo
usage
;;
1)
run_normal $1
;;
2)
case $2 in
now)
run_normal $1 $2
;;
conc)
run_concurrent $1 $2
;;
*)
run_else "$@"
;;
esac
;;
*)
run_else "$@"
;;
esac
}
main "$@"
exit 0