【发布时间】:2017-04-19 17:29:33
【问题描述】:
我在 Linux 中有如下的 shell 脚本
#!/bin/bash
LOG_LOCATION=/home/$USER/logs
exec > >(tee /home/$USER/logs/"$1") 2>&1
[ $# -ne 1 ] && { echo "Usage : $0 table ";exit 1; }
table=$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
}
`hive -e "create table testing.${table} as select * from fishing.${table}"`
cp /home/$USER/logs/"$1" /home/$USER/debug/"$1"
g_STATUS=$?
log_status $g_STATUS "Hive create ${table}"
echo "***********************************************************************************************************************************************************************"
如果我的 shell 脚本中有这个
exec 2>&1 | tee /home/logging/"$1"
然后我只在控制台而不是重定向文件上获取日志。
如果我的脚本中有这个
exec> /home/logging/"$1" 2>&1
然后我在重定向文件上有日志,但在控制台上没有。
如何在控制台和重定向文件上同时记录日志
【问题讨论】:
-
你想做什么?运行 1 美元?
-
尝试在
exec之前包含unbuffer... -
@kabanus $1 是我将传递给脚本的变量
-
也许这个答案可以解决你的问题https://unix.stackexchange.com/a/145654
标签: linux bash shell stdout stderr