我有一个 shell 脚本。在此脚本中,我正在读取文件的表名并执行命令。
脚本运行良好。我能够对文件中的所有表执行命令。
shell 脚本
#!/bin/bash
[ $# -ne 1 ] && { echo "Usage : $0 input file "; exit 1; }
args_file=$1
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=$1
message=$2
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 logs
和stdout
日志。我想单独收集文件中每个表的日志。
我想知道对于文件中的每个表,spark-submit
的执行是成功还是失败。说出状态日志
如何为每个表单独收集 stdout
文件并将它们存储在 Linux
中的某个位置。
我需要做哪些改变才能实现我的结果。
最佳答案
确保只重定向(stdout
)为每个 table
生成的日志将脚本中的实例复制到 /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_name>_dump.log
您可以将其更改为您想要的任何方式。
一些最佳实践是使用 -r
在 read
中标记和双引号 shell 变量。
答案更新为重定向 stderr
也到日志文件。
关于linux - 如何从文件中获取传递给 shell 脚本的单个参数的日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43673864/