From 1d4fec3979ee472c5aba17c2e5476fff6f4e02b5 Mon Sep 17 00:00:00 2001 From: hanhh <18330117883@163.com> Date: Sun, 22 Aug 2021 16:05:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9task.sh=E4=B8=AD=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shell/task.sh | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/shell/task.sh b/shell/task.sh index a6564608..36b123da 100755 --- a/shell/task.sh +++ b/shell/task.sh @@ -7,14 +7,14 @@ dir_shell=/ql/shell ## 选择python3还是node define_program() { - local p1=$1 - if [[ $p1 == *.js ]]; then + local first_param=$1 + if [[ $first_param == *.js ]]; then which_program="node" - elif [[ $p1 == *.py ]]; then + elif [[ $first_param == *.py ]]; then which_program="python3" - elif [[ $p1 == *.sh ]]; then + elif [[ $first_param == *.sh ]]; then which_program="bash" - elif [[ $p1 == *.ts ]]; then + elif [[ $first_param == *.ts ]]; then which_program="ts-node-transpile-only" else which_program="" @@ -75,23 +75,23 @@ run_nohup() { ## 正常运行单个脚本,$1:传入参数 run_normal() { - local p1=$1 + local first_param=$1 cd $dir_scripts - define_program "$p1" - if [[ $p1 == *.js ]]; then + define_program "$first_param" + if [[ $first_param == *.js ]]; then if [[ $# -eq 1 ]]; then random_delay fi fi log_time=$(date "+%Y-%m-%d-%H-%M-%S") - log_dir_tmp="${p1##*/}" + log_dir_tmp="${first_param##*/}" log_dir="$dir_log/${log_dir_tmp%%.*}" log_path="$log_dir/$log_time.log" cmd=">> $log_path" [[ "$show_log" == "true" ]] && cmd="" make_dir "$log_dir" - local id=$(cat $list_crontab_user | grep -E "$cmd_task $p1" | perl -pe "s|.*ID=(.*) $cmd_task $p1\.*|\1|" | head -1 | awk -F " " '{print $1}') + 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 begin_time=$(date '+%Y-%m-%d %H:%M:%S') eval echo -e "## 开始执行... $begin_time\n" $cmd eval cat $task_error_log_path $cmd @@ -99,7 +99,7 @@ run_normal() { [[ $id ]] && update_cron "\"$id\"" "0" "$$" "$log_path" . $file_task_before $cmd 2>&1 - eval timeout -k 10s $command_timeout_time $which_program $p1 $cmd 2>&1 + eval timeout -k 10s $command_timeout_time $which_program $first_param $cmd 2>&1 eval . $file_task_after $cmd 2>&1 [[ $id ]] && update_cron "\"$id\"" "1" "" "$log_path" @@ -110,37 +110,37 @@ run_normal() { ## 并发执行时,设定的 RandomDelay 不会生效,即所有任务立即执行 run_concurrent() { - local p1=$1 - local p3=$3 - if [[ ! $p3 ]]; then + local first_param=$1 + local third_param=$3 + if [[ ! $third_param ]]; then echo -e "\n 缺少并发运行的环境变量参数" exit 1 fi cd $dir_scripts - define_program "$p1" + define_program "$first_param" log_time=$(date "+%Y-%m-%d-%H-%M-%S") - log_dir_tmp="${p1##*/}" + log_dir_tmp="${first_param##*/}" log_dir="$dir_log/${log_dir_tmp%%.*}" log_path="$log_dir/$log_time.log" cmd=">> $log_path" [[ "$show_log" == "true" ]] && cmd="" make_dir $log_dir - local id=$(cat $list_crontab_user | grep -E "$cmd_task $p1" | perl -pe "s|.*ID=(.*) $cmd_task $p1\.*|\1|" | head -1 | awk -F " " '{print $1}') + 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 begin_time=$(date '+%Y-%m-%d %H:%M:%S') eval echo -e "## 开始执行... $begin_time\n" $cmd eval cat $task_error_log_path $cmd [[ $id ]] && update_cron "\"$id\"" "0" "$$" "$log_path" eval . $file_task_before $cmd 2>&1 - local envs=$(eval echo "\$${p3}") + local envs=$(eval echo "\$${third_param}") local array=($(echo $envs | sed 's/&/ /g')) single_log_time=$(date "+%Y-%m-%d-%H-%M-%S.%N") for i in "${!array[@]}"; do - export ${p3}=${array[i]} + export ${third_param}=${array[i]} single_log_path="$log_dir/${single_log_time}_$((i + 1)).log" - timeout -k 10s $command_timeout_time $which_program $p1 &>$single_log_path 2>&1 & + timeout -k 10s $command_timeout_time $which_program $first_param &>$single_log_path 2>&1 & done wait @@ -167,7 +167,7 @@ run_else() { [[ "$show_log" == "true" ]] && cmd="" make_dir "$log_dir" - local id=$(cat $list_crontab_user | grep -E "$cmd_task $p1" | perl -pe "s|.*ID=(.*) $cmd_task $p1\.*|\1|" | head -1 | awk -F " " '{print $1}') + 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 begin_time=$(date '+%Y-%m-%d %H:%M:%S') eval echo -e "## 开始执行... $begin_time\n" $cmd eval cat $task_error_log_path $cmd