管道式stderr,但日志文件中出现一些stdout行

问题描述 投票:0回答:1

我已在

crontab
中尝试了以下两个选项,并期望在日志文件中仅获得
stderr
。但是,脚本文件中与 mongodump 命令相关的一些非错误行出现在日志文件中。我在这里看到了有关 stderr 管道的各种帖子,但无法得到我想要的。我哪里错了?

尝试过的两个 crontab 选项:

0 0 * * * /root/scripts/dev-vps_backup.sh >> logfile.log 2>&1 > /dev/null

0 0 * * * /root/scripts/dev-vps_backup.sh 2>&1 >> all.log | tee -a all.log err.log >&2

bash 脚本文件:

#!/bin/bash

set -e

#start time var
start_time=$(TZ=Asia/Calcutta date +%d-%m-%Y_%H-%M-%S)
echo "Started Script: " $(basename "$0") "@ $start_time"

#########UPDATE THIS###########
### setting up variables here
######################## END of UPDATE

#create backup folders
mkdir -p $local_backupfolder
mkdir -p $local_dumps_backupfolder
mkdir -p $local_assets_backupfolder

# remote backup storage
remote_base_backupfolder=root@$remote_IP:/root/r-backups/$vps_name


# mongodump
gzfile=$local_dumps_backupfolder/$vps_name--dump--$start_time.gz
dumpMsg="$vps_name mongodump StartTime:$start_time, EndTime:"

if mongodump --gzip --archive=$gzfile ; then   ### THIS generates lines that are shown in stderr file
  echo "DONE: $dumpMsg $(TZ=Asia/Calcutta date +%d-%m-%Y_%H-%M-%S)"
else
  echo "ERR: $dumpMsg $(TZ=Asia/Calcutta date +%d-%m-%Y_%H-%M-%S)" | mailx -s "ERR: $vps_name mongodump" $recipient_email
  exit
fi

zipMsg1="$vps_name - $app_name1 - ZIP file for folder: $local_assets_folder1. StartTime:$start_time, EndTime:"

if zip -r $local_assets_backupfolder/$vps_name--$app_name1--assets-$(TZ=Asia/Calcutta date +%d-%m-%Y_%H-%M-%S).zip $local_assets_folder1 ; then
   echo "DONE: $zipMsg1 $(TZ=Asia/Calcutta date +%d-%m-%Y_%H-%M-%S)"
else
   echo "ERR: $zipMsg1 $(TZ=Asia/Calcutta date +%d-%m-%Y_%H-%M-%S)" | mailx -s "ERR: $vps_name - $app_name1 - assets local ZIP" $recipient_email
   exit
fi

some1-some_command  # to generate error

## some more lines

err.log 文件:

2024-08-02T10:33:01.116+0200    writing admin.system.version to archive '/root/backups/dumps/fvc1-vps--dump--02-08-2024_14-03-01.gz'
2024-08-02T10:33:01.118+0200    done dumping admin.system.version (1 document)
2024-08-02T10:33:01.118+0200    writing myca.syslogs to archive '/root/backups/dumps/fvc1-vps--dump--02-08-2024_14-03-01.gz'
.....some more lines related to mongodump command....
/root/scripts/fvc1-vps_backup.sh: line 61: some1-some_command: command not found

all.log 文件包含与

echo
命令和
zip
命令相关的附加行。

预期:只有 err.log 文件中的最后一行 - 这是错误。我如何实现这一目标?

更新

尝试过这个crontab:

59 11 * * * /root/scripts/fvc1-vps_backup.sh >> /root/scripts/logfile.log

生成的日志文件:

Started Script:  fvc1-vps_backup.sh @ 02-08-2024_15-29-01
DONE: fvc1-vps mongodump StartTime:02-08-2024_15-29-01, EndTime: 02-08-2024_15-29-01
  adding: root/jobs/assets/ (stored 0%)
  adding: root/jobs/assets/tempfiles/ (stored 0%)
        ...... some more lines resulting from zip command.....
  adding: root/jobs/assets/invoiceDocs/ (stored 0%)
DONE: fvc1-vps - jobs - ZIP file for folder: /root/jobs/assets. StartTime:02-08-2024_15-29-01, EndTime: 02-08-2024_15-29-19

日志文件不包含 mongodump 相关行。这意味着这些行将进入 stderr(如第一个日志文件结果中所示)。不知道为什么?

linux bash pipe stderr
1个回答
0
投票

第二组重定向似乎适合您想要的:

shellscript 2>&1 >> all.log | tee -a all.log err.log >&2

但是,它依赖于

shellscript
中所有单独命令的输出,到达您想要的位置(stdout 或 stderr)。

看起来

mongodump
写入了 stderr。如果您希望其输出出现在
all.log
而不是
err.log
中,则需要将其 stderr 重定向到 stdout。例如:

if mongodump --gzip --archive=$gzfile 2>&1 ; then
© www.soinside.com 2019 - 2024. All rights reserved.