script输出将旧数据覆盖到write to new line的输出文件中

bfrts1fy  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(262)

我附带了一个脚本来检查名称节点的状态。当我手动运行时,namenode的状态会写入一个文件,当我再次运行时,第二次尝试输出会在旧文件中添加新行。但我已经在cron中计划每15分钟运行一次。但是cron作业覆盖了旧数据,我只能看到上次运行的状态。如何通过cron添加新行?


# !/bin/bash

NOW=$(date +"%m-%d-%Y")
fname=active.$NOW.log
[ ! -f  $fname ] && > /opt/hd/sh/bin/$fname

if [[ $(hdfs haadmin -ns ATcluster -getServiceState nn1) = *active* ]];
then
  echo "`date +"%Y-%m-%d %H:%M:%S"` active NN1" >> /opt/hd/sh/bin/$fname
elif [[ $(hdfs haadmin -ns ATcluster -getServiceState nn2) = *active* ]];
then
  echo "`date +"%Y-%m-%d %H:%M:%S"` active NN2" >> /opt/hd/sh/bin/$fname
else
  echo "`date +"%Y-%m-%d %H:%M:%S"` stopped state" >> /opt/hd/sh/bin/failed.$NOW.log
fi

输出:

cat active.01-1
cat: active.01-1: No such file or directory
[root@hadmat01 bin]# cat active.01-11-2018.log
2018-01-11 06:00:04 active NN1 --> it should write nw output like below through cron job
[root@hadmat01 bin]# cat failed.01-11-2018.log
20180111 00:00:05 stopped state
20180111 00:15:05 stopped state
20180111 00:30:05 stopped state
20180111 00:45:05 stopped state
pzfprimi

pzfprimi1#

您的问题在以下代码中: fname=active.$NOW.log [ ! -f $fname ] && > /opt/hd/sh/bin/$fname 如果此作业是从crontab启动的-检查文件是否存在从 / . 我认为location/active.01-11-2018.log不存在。所以你把emty string放在文件里了 /opt/hd/sh/bin/$fname 必须为日志目标定义绝对路径,或插入字符串 cd /opt/hd/sh/bin/ 之前 if 部分

相关问题