![](/img/trans.png)
[英]How to create shell script for creating logs on linux server based on hostname
[英]How to create status logs for shell script in Linux/bash
我有一個shell腳本。 我要向該腳本傳遞文件中的參數。 該文件包含表名稱
該腳本運行正常。 我可以為文件中的所有表執行命令。
shell script
#!/bin/bash
[ $# -ne 1 ] && { echo "Usage : $0 input file "; exit 1; }
input_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
sqoop job --exec $table > /home/$USER/logging/"${table}_log" 2>&1
done < ${input_file}
g_STATUS=$?
log_status $g_STATUS "Sqoop job ${table}"
我正在嘗試收集腳本的status logs
。
我想分別收集每個表的status logs
。
我想要的是
2017-04-28 20:36:41 [ERROR] sqoop job table1 EXECUTION [Status] 2 : failed
2017-04-28 20:36:41 [ERROR] sqoop job table2 EXECUTION [Status] 2 : failed
我得到什么
如果最后一張表的腳本失敗
2017-04-28 20:38:41 [ERROR] sqoop job EXECUTION [Status] 2 : failed
如果最后一個表的腳本成功,則
2017-04-28 20:40:41 [ERROR] sqoop job [Status] 0 : success
我在做什么錯,應該做出哪些更改才能獲得理想的結果。
更改
while read table ;do
sqoop job --exec $table > /home/$USER/logging/"${table}_log" 2>&1
done < ${input_file}
g_STATUS=$?
log_status $g_STATUS "Sqoop job ${table}"
至
while read table ;do
sqoop job --exec $table > /home/$USER/logging/"${table}_log" 2>&1
g_STATUS=$?
log_status $g_STATUS "Sqoop job ${table}"
# Any other command you want to run on using $table should be placed here
done < ${input_file}
while
循環僅在while
和done
行中運行代碼。 因此,要記錄所有表,您需要在while循環中運行記錄。
同樣, $table
在循環的迭代中發生更改,因此要在所有表上運行的任何命令都需要在循環內運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.