簡體   English   中英

如何從文件中獲取傳遞給shell腳本的單個參數的日志

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

我有一個shell腳本。 在這個腳本中,我正在讀取文件的表名並執行命令。

腳本運行正常。 我能夠為文件中的所有表執行命令。

shell script

#!/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 logsstdout日志。 我想單獨收集文件中每個表的日志。

我想知道文件中每個表的spark-submit執行是成功還是失敗。 說出status logs

如何單獨收集每個表的stdout文件並將它們存儲在Linux中的某個位置。

我需要做些什么來實現我的結果。

確保只是將為腳本中的每個table實例生成的日志myScriptLogsstdout )到/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 /var/log <table_name>_dump.log創建一個日志,您可以按照自己的方式將其更改為<table_name>_dump.log

最佳實踐的幾個方法是在read和雙引用shell變量中使用-r標志。


更新后更新以將stderr重定向到日志文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM