Bash 用于在特定时间间隔内复制/tmp目录中的日志文件的shell脚本
我想编写一个shell脚本,将在1小时内生成的日志文件复制到/tmp目录。我希望在cron中安排此脚本,以便此作业可以每小时运行一次,并将新生成的日志文件复制到/tmp中Bash 用于在特定时间间隔内复制/tmp目录中的日志文件的shell脚本,bash,shell,Bash,Shell,我想编写一个shell脚本,将在1小时内生成的日志文件复制到/tmp目录。我希望在cron中安排此脚本,以便此作业可以每小时运行一次,并将新生成的日志文件复制到/tmp中 谢谢,您将一个文件作为参数,它会显示正在发生的日志。。。 您可以将脚本更改为从标准输出重定向到文件 #!/bin/bash GAP=10 #How long to wait LOGFILE=$1 #File to log to if [ "$#" -ne "1" ] then echo "USAGE: ./
谢谢,您将一个文件作为参数,它会显示正在发生的日志。。。 您可以将脚本更改为从标准输出重定向到文件
#!/bin/bash
GAP=10 #How long to wait
LOGFILE=$1 #File to log to
if [ "$#" -ne "1" ]
then
echo "USAGE: ./watch-log.sh <file with absolute path>"
exit 1
fi
#Get current long of the file
len=`wc -l $LOGFILE | awk '{ print $1 }'`
echo "Current size is $len lines."
while :
do
if [ -N $LOGFILE ]
then
echo "`date`: New Entries in $LOGFILE: "
newlen=`wc -l $LOGFILE | awk ' { print $1 }'`
newlines=`expr $newlen - $len`
tail -$newlines $LOGFILE
len=$newlen
fi
sleep $GAP
done
exit 0
#/bin/bash
差距=10#等待多长时间
LOGFILE=$1#要登录的文件
如果[“$#”-ne“1”]
然后
echo“用法:./watch-log.sh”
出口1
fi
#获取文件的当前长度
len=`wc-l$LOGFILE | awk'{print$1}'`
echo“当前大小为$len行。”
而:
做
如果[-N$LOGFILE]
然后
echo“`date`:$LOGFILE中的新条目:
newlen=`wc-l$LOGFILE | awk'{print$1}'`
换行符=`expr$newlen-$len`
尾部-$newlines$LOGFILE
len=$newlen
fi
睡眠$GAP
完成
出口0
假设/tmp不在搜索路径中,使用find命令将能够实现上述功能
日志文件的扩展名为.log
find $PATH_TO_SEARCH -type f -name "*.log" -cmin -60 -exec cp {} /tmp \;
# Above single command can be configured as cron job.