From d687e79b6c89f458087b6cb5eef5908598b74c46 Mon Sep 17 00:00:00 2001 From: whyour Date: Sun, 3 Apr 2022 19:32:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A5=E5=BF=97=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=8B=BC=E6=8E=A5=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/services/cron.ts | 12 ++++++- shell/task.sh | 80 +++++++++++++++++++++++++++---------------- 2 files changed, 61 insertions(+), 31 deletions(-) diff --git a/back/services/cron.ts b/back/services/cron.ts index 1a7dd6c8..19c799f2 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -408,7 +408,17 @@ export default class CronService { command.lastIndexOf('.') !== -1 ? command.lastIndexOf('.') : command.length; - return command.substring(start, end); + + const tmpStr = command.startsWith('/') ? command.substring(1) : command; + const index = tmpStr.indexOf('/') !== -1 ? tmpStr.indexOf('/') : 0; + if (index) { + console.log( + `${tmpStr.substring(0, index)}_${command.substring(start, end)}`, + ); + return `${tmpStr.substring(0, index)}_${command.substring(start, end)}`; + } else { + return command.substring(start, end); + } } private make_command(tab: Crontab) { diff --git a/shell/task.sh b/shell/task.sh index 00dfae9d..4d9a8aa8 100755 --- a/shell/task.sh +++ b/shell/task.sh @@ -7,14 +7,14 @@ dir_shell=$QL_DIR/shell ## 选择python3还是node define_program() { - local first_param=$1 - if [[ $first_param == *.js ]]; then + local file_param=$1 + if [[ $file_param == *.js ]]; then which_program="node" - elif [[ $first_param == *.py ]] || [[ $first_param == *.pyc ]]; then + elif [[ $file_param == *.py ]] || [[ $file_param == *.pyc ]]; then which_program="python3" - elif [[ $first_param == *.sh ]]; then + elif [[ $file_param == *.sh ]]; then which_program="bash" - elif [[ $first_param == *.ts ]]; then + elif [[ $file_param == *.ts ]]; then which_program="ts-node-transpile-only" else which_program="" @@ -76,20 +76,25 @@ run_nohup() { ## 正常运行单个脚本,$1:传入参数 run_normal() { - local first_param=$1 - define_program "$first_param" - if [[ $first_param == *.js ]]; then + local file_param=$1 + define_program "$file_param" + if [[ $file_param == *.js ]]; then if [[ $# -eq 1 ]]; then random_delay fi fi log_time=$(date "+%Y-%m-%d-%H-%M-%S") - log_dir_tmp="${first_param##*/}" - log_dir_tmp_path="${first_param%%/*}" + log_dir_tmp="${file_param##*/}" + if [[ $file_param == /* ]]; then + log_dir_tmp_path="${file_param:1}" + else + log_dir_tmp_path="${file_param}" + fi + log_dir_tmp_path="${log_dir_tmp_path%%/*}" log_dir_tmp_path="${log_dir_tmp_path##*/}" [[ $log_dir_tmp_path ]] && log_dir_tmp="${log_dir_tmp_path}_${log_dir_tmp}" - log_dir="${log_dir_tmp%%.*}" + log_dir="${log_dir_tmp%.*}" log_path="$log_dir/$log_time.log" cmd="&>> $dir_log/$log_path" [[ "$show_log" == "true" ]] && cmd="" @@ -100,17 +105,17 @@ run_normal() { eval echo -e "\#\# 开始执行... $begin_time\\\n" $cmd [[ -f $task_error_log_path ]] && eval cat $task_error_log_path $cmd - local id=$(cat $list_crontab_user | grep -E "$cmd_task $first_param" | perl -pe "s|.*ID=(.*) $cmd_task $first_param\.*|\1|" | head -1 | awk -F " " '{print $1}') + local id=$(cat $list_crontab_user | grep -E "$cmd_task $file_param" | perl -pe "s|.*ID=(.*) $cmd_task $file_param\.*|\1|" | head -1 | awk -F " " '{print $1}') [[ $id ]] && update_cron "\"$id\"" "0" "$$" "$log_path" "$begin_timestamp" eval . $file_task_before "$@" $cmd cd $dir_scripts - local relative_path="${first_param%/*}" - if [[ ! -z ${relative_path} ]] && [[ ${first_param} =~ "/" ]]; then + local relative_path="${file_param%/*}" + if [[ ! -z ${relative_path} ]] && [[ ${file_param} =~ "/" ]]; then cd ${relative_path} - first_param=${first_param/$relative_path\//} + file_param=${file_param/$relative_path\//} fi - eval timeout -k 10s $command_timeout_time $which_program $first_param $cmd + eval timeout -k 10s $command_timeout_time $which_program $file_param $cmd eval . $file_task_after "$@" $cmd local end_time=$(date '+%Y-%m-%d %H:%M:%S') @@ -122,7 +127,7 @@ run_normal() { ## 并发执行时,设定的 RandomDelay 不会生效,即所有任务立即执行 run_concurrent() { - local first_param="$1" + local file_param="$1" local env_param="$2" local num_param=$(echo "$3" | perl -pe "s|.*$2(.*)|\1|") if [[ ! $env_param ]]; then @@ -145,13 +150,18 @@ run_concurrent() { local cookieStr=$(echo ${array_run[*]} | sed 's/\ /\&/g') [[ ! -z $cookieStr ]] && export ${env_param}=${cookieStr} - define_program "$first_param" + define_program "$file_param" log_time=$(date "+%Y-%m-%d-%H-%M-%S") - log_dir_tmp="${first_param##*/}" - log_dir_tmp_path="${first_param%%/*}" + log_dir_tmp="${file_param##*/}" + if [[ $file_param == /* ]]; then + log_dir_tmp_path="${file_param:1}" + else + log_dir_tmp_path="${file_param}" + fi + log_dir_tmp_path="${log_dir_tmp_path%%/*}" log_dir_tmp_path="${log_dir_tmp_path##*/}" [[ $log_dir_tmp_path ]] && log_dir_tmp="${log_dir_tmp_path}_${log_dir_tmp}" - log_dir="${log_dir_tmp%%.*}" + log_dir="${log_dir_tmp%.*}" log_path="$log_dir/$log_time.log" cmd="&>> $dir_log/$log_path" [[ "$show_log" == "true" ]] && cmd="" @@ -163,7 +173,7 @@ run_concurrent() { eval echo -e "\#\# 开始执行... $begin_time\\\n" $cmd [[ -f $task_error_log_path ]] && eval cat $task_error_log_path $cmd - local id=$(cat $list_crontab_user | grep -E "$cmd_task $first_param" | perl -pe "s|.*ID=(.*) $cmd_task $first_param\.*|\1|" | head -1 | awk -F " " '{print $1}') + local id=$(cat $list_crontab_user | grep -E "$cmd_task $file_param" | perl -pe "s|.*ID=(.*) $cmd_task $file_param\.*|\1|" | head -1 | awk -F " " '{print $1}') [[ $id ]] && update_cron "\"$id\"" "0" "$$" "$log_path" "$begin_timestamp" eval . $file_task_before "$@" $cmd @@ -172,15 +182,15 @@ run_concurrent() { single_log_time=$(date "+%Y-%m-%d-%H-%M-%S.%N") cd $dir_scripts - local relative_path="${first_param%/*}" - if [[ ! -z ${relative_path} ]] && [[ ${first_param} =~ "/" ]]; then + local relative_path="${file_param%/*}" + if [[ ! -z ${relative_path} ]] && [[ ${file_param} =~ "/" ]]; then cd ${relative_path} - first_param=${first_param/$relative_path\//} + file_param=${file_param/$relative_path\//} fi for i in "${!array[@]}"; do export ${env_param}=${array[i]} single_log_path="$log_dir/${single_log_time}_$((i + 1)).log" - timeout -k 10s $command_timeout_time $which_program $first_param &>$single_log_path & + timeout -k 10s $command_timeout_time $which_program $file_param &>$single_log_path & done wait @@ -210,10 +220,15 @@ run_designated() { define_program "$file_param" log_time=$(date "+%Y-%m-%d-%H-%M-%S") log_dir_tmp="${file_param##*/}" - log_dir_tmp_path="${file_param%%/*}" + if [[ $file_param == /* ]]; then + log_dir_tmp_path="${file_param:1}" + else + log_dir_tmp_path="${file_param}" + fi + log_dir_tmp_path="${log_dir_tmp_path%%/*}" log_dir_tmp_path="${log_dir_tmp_path##*/}" [[ $log_dir_tmp_path ]] && log_dir_tmp="${log_dir_tmp_path}_${log_dir_tmp}" - log_dir="${log_dir_tmp%%.*}" + log_dir="${log_dir_tmp%.*}" log_path="$log_dir/$log_time.log" cmd="&>> $dir_log/$log_path" [[ "$show_log" == "true" ]] && cmd="" @@ -266,10 +281,15 @@ run_else() { define_program "$file_param" log_time=$(date "+%Y-%m-%d-%H-%M-%S") log_dir_tmp="${file_param##*/}" - log_dir_tmp_path="${file_param%%/*}" + if [[ $file_param == /* ]]; then + log_dir_tmp_path="${file_param:1}" + else + log_dir_tmp_path="${file_param}" + fi + log_dir_tmp_path="${log_dir_tmp_path%%/*}" log_dir_tmp_path="${log_dir_tmp_path##*/}" [[ $log_dir_tmp_path ]] && log_dir_tmp="${log_dir_tmp_path}_${log_dir_tmp}" - log_dir="${log_dir_tmp%%.*}" + log_dir="${log_dir_tmp%.*}" log_path="$log_dir/$log_time.log" cmd="&>> $dir_log/$log_path" [[ "$show_log" == "true" ]] && cmd=""