I need to run a shell script in every 10 minutes. I am appending the output in a log file every time script runs. I need to stop the script from running once the size of the log file reached to 10 MB. how can this be achieved , please help !!
我需要每10分钟运行一次shell脚本。每次脚本运行时,我都会将输出附加到日志文件中。一旦日志文件的大小达到10 MB,我需要停止脚本运行。怎么能实现,请帮助!!
./test.sh >> log_file
3 个解决方案
#1
1
maximumsize=10000 #KB
while :
do
./test.sh >> log_file
actualsize=$(du -k log_file | cut -f 1)
if [ $actualsize -gt $maximumsize ];then
echo "logsize exceed.stop."
break
fi
sleep 600
done
#2
0
You should modify your test script : put your process into a function, add your function into a loop, condition of the loop (size of your log file under 10MB) and at the end of the loop add a sleep 600
to wait for ten minutes
您应该修改您的测试脚本:将您的进程放入一个函数,将您的函数添加到循环中,循环的条件(日志文件的大小低于10MB)并在循环结束时添加一个休眠600等待十分钟
#3
0
You might add to the start of your test.sh script a test, if the log file > 10MB then do not run (exit), or you could archive (maybe gzip -9
or xz -9
) the log, if it's all text output then it should compress a LOT. Or you could pipe the script's output through gzip to compress it right away.
您可以在test.sh脚本的开头添加一个测试,如果日志文件> 10MB然后不运行(退出),或者您可以存档(可能是gzip -9或xz -9)日志,如果它是全文输出那么它应该压缩很多。或者你可以通过gzip管道脚本的输出来立即压缩它。
And also use cron/anacron to run the script regularly, like add */10 * * * * user test.sh
to /etc/crontab...
并且还使用cron / anacron定期运行脚本,例如将* / 10 * * * * user test.sh添加到/ etc / crontab ...
#1
1
maximumsize=10000 #KB
while :
do
./test.sh >> log_file
actualsize=$(du -k log_file | cut -f 1)
if [ $actualsize -gt $maximumsize ];then
echo "logsize exceed.stop."
break
fi
sleep 600
done
#2
0
You should modify your test script : put your process into a function, add your function into a loop, condition of the loop (size of your log file under 10MB) and at the end of the loop add a sleep 600
to wait for ten minutes
您应该修改您的测试脚本:将您的进程放入一个函数,将您的函数添加到循环中,循环的条件(日志文件的大小低于10MB)并在循环结束时添加一个休眠600等待十分钟
#3
0
You might add to the start of your test.sh script a test, if the log file > 10MB then do not run (exit), or you could archive (maybe gzip -9
or xz -9
) the log, if it's all text output then it should compress a LOT. Or you could pipe the script's output through gzip to compress it right away.
您可以在test.sh脚本的开头添加一个测试,如果日志文件> 10MB然后不运行(退出),或者您可以存档(可能是gzip -9或xz -9)日志,如果它是全文输出那么它应该压缩很多。或者你可以通过gzip管道脚本的输出来立即压缩它。
And also use cron/anacron to run the script regularly, like add */10 * * * * user test.sh
to /etc/crontab...
并且还使用cron / anacron定期运行脚本,例如将* / 10 * * * * user test.sh添加到/ etc / crontab ...