如何在HDFS中存储shell脚本的日志文件

时间:2017-04-10 20:40:47

标签: linux shell hadoop hdfs

我在HDFS中有shell脚本。我想仅在HDFS中收集此脚本的日志。

脚本的内容如下:

#!/bin/bash

TIMESTAMP=`date "+%Y-%m-%d"`

hdfs dfs -touchz /user/$USER/logs/`date "+%Y-%m-%d"`/${TIMESTAMP}.success_log

hdfs dfs -touchz /user/$USER/logs/`date "+%Y-%m-%d"`/${TIMESTAMP}.fail_log

success_logs=/user/$USER/logs/`date "+%Y-%m-%d"`/${TIMESTAMP}.success_log

failed_logs=/user/$USER/logs/`date "+%Y-%m-%d"`/${TIMESTAMP}.fail_log


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
}

日志不会附加到文件中。只创建文件。

如何让文件在HDFS

中附加函数的结果

1 个答案:

答案 0 :(得分:0)

  

日志不会附加到文件中。只有文件正在   创建

因为tee是linux命令,不适用于存储在HDFS中的文件。

使用-appendToFile

echo "`date +\"%Y-%m-%d %H:%M:%S\"` [ERROR] $message [Status] $status : failed" | hdfs dfs -appendToFile - ${failed_logs}

-代替srcfile是从stdin读取输入。

相关问题