gpt4 book ai didi

linux - 打印 shell 脚本的控制台日志

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:40:36 24 4
gpt4 key购买 nike

我有一个如下所示的 shell 脚本。我想将控制台的完整日志保存到一个文件中。

#!/bin/bash
#This script is to import tables from mysql to hdfs

source /home/$USER/source.sh

[ $# -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 test_1 as select * from db.test"`

g_STATUS=$?
log_status $g_STATUS "Table created ${table}"
echo "******************************************************************************"

当我运行脚本时,日志仅显示作业是成​​功还是失败。如何将整个控制台日志保存到文件中。

如何将状态日志保存到一个文件,控制台日志保存到另一个文件

最佳答案

利用两个文件描述符,指向两个不同的文件,然后将相关日志重定向到所需的文件描述符。

例如,将其放在 shebang 之后:

exec 3>console.log
exec 4>status.log

现在,要将所有控制台日志发送到 console.log,请执行以下操作:

echo .... >&3

类似地,对于所有状态日志:

echo ... >&4

更改文件名以满足您的需要。

关于linux - 打印 shell 脚本的控制台日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43197619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com