我正在运行一个清除进程,我使用的命令是:
find $sentPurgerFolder -mtime +7 -print -delete >> $sentPurgeLogFile 2>&1
此代码位于 while 循环中,该循环遍历具有多个要清除的数据文件夹的客户端。目的是将所有清除信息(无论该客户端的文件夹数量如何)发送到该客户端的一个日志文件。这部分似乎工作得很好。
我的断开是我想将相同的输出发送到主日志文件,但是我看到的“tee”示例让我暂停。我不知道如何在不将日志数据加倍的情况下将该命令集成到我的代码中。
有人可以提供一些见解或提出建议吗?
答案1
将每个管道传输find ... 2>&1
到tee -a "$sentPurgeLogFile"
.这会将命令的输出附加find
到值给定的文件中$sentPurgeLogFile
,但也会将相同的输出发送到循环的标准输出while
。
将循环的输出重定向while
到“master”日志文件:
while ...; do
# find commands here, e.g.
find "$sentPurgerFolder" -mtime +7 -print -delete 2>&1 | tee -a "$sentPurgeLogFile"
done >master.log
这样,您可以将每个客户端的输出存储在其自己的日志文件中,同时还将所有输出存储在master.log
.
目前尚不清楚该while
循环是运行单个循环find
还是多个循环。如果它运行单个find
,请删除每次运行该命令时为该客户端创建新日志文件-a
的选项。tee
如果您运行多个find
命令,您可能只想对第一个执行此操作tee
。显然,除非您想存储脚本所有调用的累积日志(在这种情况下,您可能还想>>master.log
在循环之后使用而不是>master.log
)。
答案2
好吧,我会咬的。鉴于我想象你正在尝试实现的目标。这是我可能尝试使用的。
我的假设:
- 将结果转储(附加)到主日志
- 目录可能有子目录
#!/bin/sh -
masterlog="/some/place/some.file"
dirlist="
/one/place
/two/place
/three/place
"
for d in $dirlist
do
cd $d
for f in `find . -mtime +7 -maxdepth 1 -type f`
do
cat $f >>$masterlog
echo ''>$f
done
done
exit
未经测试
我想象您试图做的是将所有单独日志的内容收集到 中$masterlog
,然后清空这些单独日志的内容。
如果我是对的。然后这就完成了。 :)
华泰
答案3
我找到了另一种方法,它完全否定了这篇文章,但我会尝试说明我所做的......
所以我有一个由“%”分隔的客户端配置文件。这是由 sftp 进程逐行读取的,该进程将向客户端推送数据或从客户端拉取数据。它看起来像这样:
...
client_1%FileContent_1%xml%...(client connection info)
client_1%FileContent_2%txt%...(client connection info)
client_1%FileContent_3%pdf%...(client connection info)
client_2%FileContent_1%xml%...(client connection info)
client_2%FileContent_2%pdf%...(client connection info)
...
所以我的清除过程会读取此内容并查看前三个字段。它们用于为每个客户端组装基本路径。
/xmit/client/outbound
现在,上述文件夹每个都有一个“已发送”或“已接收”和“日志”文件夹。
/xmit/client/outbound/sent
/xmit/client/outbound/log
此 sftp 进程为这些客户发送和接收文件,当处理每个文件时,它将所述文件放入 /sent 和 /received 文件夹中,并将所述文件的发送/接收的完整跟踪放入 /log 文件夹中。
因此,我正在编写的清除过程会读取相同的配置文件来找到要清除的文件夹,但我想合并每个客户端的清除日志文件。
#
# Inits....
#
basepath=/xmit
workingdir=$basepath/bin/Purge
purgeProcessLogFile=$workingdir/outboundPurge.log
#
cnfgFl=/xmit/bin/OutBoundScripts/clientsftpconfigoutbound.txt
# Config File Ref
#
declare -i cnfgFlNbrOfLns=`wc -l "$cnfgFl" | cut -d ' ' -f 1`
# Count lines in config file...
#
# Set beginning line and work from beginning line to ending line...
#
firstpass=1
declare -i cnfgFlStrtLn="53"
declare -i currentLnNbr=$cnfgFlStrtLn
lastClient='0'
#
# Begin while loop...
#
while [ "$currentLnNbr" -le "$cnfgFlNbrOfLns" ]
do
#
# Set what line to read from and read the data in...
#
option="$currentLnNbr"p
cnfgFlLn=`sed -n $option $cnfgFl`
#
# Increment counter for the next loop, you've allredy got the data...
#
declare -i currentLnNbr=`expr $currentLnNbr + 1`
#
# Check if very first character in config line is not a "#" and proceed.
#
fld=`echo $cnfgFlLn | cut -c1-1`
#
if [ "$fld" != \# ]
then
#
# Get file content... (ex: '7501' )
#
cntnt=`echo $cnfgFlLn | cut -d "%" -f 2`
#
# Extract account from
#
currClient=`echo $cnfgFlLn | cut -d "%" -f 1`
#
if [ $currClient != \# ]
then
#
# Check if on new account. If new account, set 'lastClient' to current account
# and reset log files...
#
if [ "$lastClient" != "$currClient" ]
then
#
lastClient=$currClient
#
# Set current date and time stamp...
#
dt=`date +%Y%m%d``date +%H%M%S`
#
# Set sent purge log...
#
sentPurgeFolder=$basepath/$currClient/outbound/sent
sentPurgeLogFile=$sentPurgeFolder/SentPurgeLogFile\_$dt.log
#
# Set sent log purge log...
#
sentLogPurgeFolder=$basepath/$currClient/outbound/logs
sentLogPurgeLogFile=$sentLogPurgeFolder/SentLogPurgeLogFile\_$dt.log
#
# Prime new logfiles...
#
cat /dev/null > $sentPurgeLogFile
cat /dev/null > $sentLogPurgeLogFile
fi
#
# See if there are any files to purge...
#
declare -i cntntFilesToPurge=`find $sentPurgeFolder -mtime +7 -print | wc -l`
if [ "$cntntFilesToPurge" -gt "0" ]
then
#
# 'cd' to sent folder, purge and upate logfile...
#
cd $sentPurgeFolder
dt1=`date +%Y%m%d``date +%H%M%S`
echo $dt1 - Purging "$cntntFilesToPurge" "'"$cntnt"'" data files that are 7 days or older from $sentPurgeFolder >> $purgeProcessLogFile
find $sentPurgeFolder -mtime +7 -print -delete >> $sentPurgeLogFile 2>&1
else
dt1=`date +%Y%m%d``date +%H%M%S`
echo $dt1" - No ""'"$cntnt"'" data files that are 7 days or older to purge from $sentPurgeFolder >> $purgeProcessLogFile
fi
#
# See if there are any logs to purge...
#
declare -i logFilesToPurge=`find $sentLogPurgeFolder -mtime +7 -print | wc -l`
if [ "$logFilesToPurge" -gt "0" ]
then
#
# 'cd' to logs folder, purge and upate logfile...
#
cd $sentLogPurgeFolder
dt1=`date +%Y%m%d``date +%H%M%S`
echo $dt1 - Purging "'"$cntnt"'" 'purge log' data are 7 days or older from $sentPurgeFolder >> $purgeProcessLogFile
find $sentLogPurgeFolder -mtime +1 -print -delete >> $sentLogPurgeLogFile 2>&1
else
dt1=`date +%Y%m%d``date +%H%M%S`
echo $dt1" - No logfiles for ""'"$cntnt"'" data that are 7 days or older to purge from $sentPurgeFolder >> $purgeProcessLogFile
fi
fi
fi
done
#
# E N D O F F I L E
#
这部分 :
declare -i cntntFilesToPurge=`find $sentPurgeFolder -mtime +7 -print | wc -l`
if [ "$cntntFilesToPurge" -gt "0" ]
then
cd $sentPurgeFolder
dt1=`date +%Y%m%d``date +%H%M%S`
echo $dt1 - Purging "$cntntFilesToPurge" "'"$cntnt"'" data files that are 7 days or older from $sentPurgeFolder >> $purgeProcessLogFile
find $sentPurgeFolder -mtime +7 -print -delete >> $sentPurgeLogFile 2>&1
else
dt1=`date +%Y%m%d``date +%H%M%S`
echo $dt1" - No ""'"$cntnt"'" data files that are 7 days or older to purge from $sentPurgeFolder >> $purgeProcessLogFile
fi
#
# 'cd' to logs folder, purge and upate logfile...
#
declare -i logFilesToPurge=`find $sentLogPurgeFolder -mtime +7 -print | wc -l`
if [ "$logFilesToPurge" -gt "0" ]
then
cd $sentLogPurgeFolder
dt1=`date +%Y%m%d``date +%H%M%S`
echo $dt1 - Purging "'"$cntnt"'" 'purge log' data are 7 days or older from $sentPurgeFolder >> $purgeProcessLogFile
find $sentLogPurgeFolder -mtime +1 -print -delete >> $sentLogPurgeLogFile 2>&1
else
dt1=`date +%Y%m%d``date +%H%M%S`
echo $dt1" - No logfiles for ""'"$cntnt"'" data that are 7 days or older to purge from $sentPurgeFolder >> $purgeProcessLogFile
fi
之前是:
#
# 'cd' to sent folder, purge and upate logfile...
#
cd $sentPurgeFolder
dt1=`date +%Y%m%d``date +%H%M%S`
find $sentPurgeFolder -mtime +7 -print -delete >> $sentPurgeLogFile 2>&1
#
# 'cd' to logs folder, purge and upate logfile...
#
cd $sentLogPurgeFolder
dt1=`date +%Y%m%d``date +%H%M%S`
find $sentLogPurgeFolder -mtime +1 -print -delete >> $sentLogPurgeLogFile 2>&1
这满足了我对“清除”日志的需求。