如何从文件中获取传递给 shell 脚本的单个参数的日志

How to get logs of individual argument passed to shell script from a file

我有一个 shell 脚本。在此脚本中,我正在读取文件的 table 名称并执行命令。

脚本运行良好。我可以为文件中的所有 table 执行命令。

shell script

#!/bin/bash

[ $# -ne 1 ] && { echo "Usage : [=11=] input file "; exit 1; }
args_file=

TIMESTAMP=`date "+%Y-%m-%d"`
touch /home/$USER/logs/${TIMESTAMP}.success_log
touch /home/$USER/logs/${TIMESTAMP}.fail_log 
success_logs=/home/$USER/logs/${TIMESTAMP}.success_log
failed_logs=/home/$USER/logs/${TIMESTAMP}.fail_log

#Function to get the status of the job creation
function log_status
{
       status=
       message=
       if [ "$status" -ne 0 ]; then
                echo "`date +\"%Y-%m-%d %H:%M:%S\"` [ERROR] $message [Status] $status : failed" | tee -a "${failed_logs}"
                #echo "Please find the attached log file for more details"
                #exit 1
                else
                    echo "`date +\"%Y-%m-%d %H:%M:%S\"` [INFO] $message [Status] $status : success" | tee -a "${success_logs}"
                fi
}

while read table ;do 
  spark-submit hive.py $table 
done < ${args_file}

g_STATUS=$?
log_status $g_STATUS "Spark ${table}"

在这个脚本中我想收集 status logsstdout 日志。我想单独收集文件中每个 table 的日志。

我想知道文件中每个 table 的 spark-submit 执行是成功还是失败。说 status logs

如何为每个 table 单独收集 stdout 个文件并将它们存储在 Linux 中的某个位置。

我需要做哪些改变才能达到我的结果。

确保将脚本中每个 table 实例生成的日志重定向 (stdout) 到 /var/log/ 下的文件夹中,可以将其称为myScriptLogs

mkdir -p /var/log/myScriptLogs || { echo "mkdir failed"; exit; }

while read -r table ;do 
  spark-submit hive.py "$table" > /var/log/myScriptLogs/"${table}_dump.log" 2>&1 
done < "${args_file}" 

如果您出于某种原因无法使用 mkdir 创建新目录,脚本将失败。因此,这会为在 /var/log 下处理的每个 table 创建一个日志,作为 <table_name>_dump.log,您可以将其更改为您想要的任何方式。

一些最佳实践是在 read 中使用 -r 标志和双引号 shell 变量。


答案更新为重定向 stderr 也到日志文件。