I have a script that I want to run every minute on my web server to check the last 3 lines of log file to see what the memory usage is for the program. The script works as in gets the correct result for the memory but I can't get it to email or exit - it just hangs?
我有一个脚本,我想在我的Web服务器上每分钟运行一次,检查最后3行日志文件,看看该程序的内存使用情况。该脚本的工作原理是获取内存的正确结果,但我无法通过电子邮件或退出 - 它只是挂起?
Any ideas on why this doesn't exit? And is this the best approach to run as a cron on the server to tail the last 3 lines of the log?
关于为什么不退出的任何想法?这是在服务器上作为cron运行的最佳方法,可以拖尾日志的最后3行吗?
#!/bin/bash
LOG=/home/user/Scripts/test.txt
SERVER=/home/user/Scripts/local/case.txt
CASE=$(head -n 1 $SERVER)
grep INFO: | grep Memory $LOG | awk '{print $9}' | tail -n 3 | while read output;
do
#echo $output
new=$(echo $output)
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
mag=$(echo $output | awk '{ print $2 }' )
if [ $new -lt 10 ]; then
echo $new
mail -s "TEST" - Alert: Memory usage is at $usep%" "user@examplecom" <<< \
" \"$CASE \" - Memory usage is currently peaked to \"$mag ($usep%)\" on $(hostname) as on $(date)"
echo "email"
fi
done
1 个解决方案
#1
1
The first grep
is reading standard input. Unless you invoke this with a redirection (please show us!) that's why it appears to hang -- it's waiting for you to type in some input.
第一个grep是读取标准输入。除非你通过重定向调用它(请告诉我们!)这就是为什么它似乎挂起 - 它等着你输入一些输入。
As a quick fix, change
作为快速解决方案,改变
grep INFO: | grep Memory $LOG | awk ...
to read
grep INFO: "$LOG" | grep Memory | awk ...
(The double quotes are not strictly necessary here, but a good idea to learn for the future.)
(双引号在这里并不是绝对必要的,但是为了将来学习是一个好主意。)
Much better, refactor the multiple grep
s and the tail
into the awk
script:
好多了,将多个greps和尾部重构为awk脚本:
awk '/INFO:/ && /Memory/ { g[NR%3+1] = $0 }
END { for (j=1; j<=3; ++j) print g[j] }' "$LOG"
If the order of the output is significant, you have to fiddle it a bit more.
如果输出的顺序很重要,你必须更多地调整它。
Also, new=$output
is vastly preferable over new=$(echo $output)
unless you specifically require word splitting, in which case set -- $output
is both safer and more idiomatic. See also http://porkmail.org/era/unix/award.html#echo
另外,new = $ output比new = $(echo $ output)更受欢迎,除非你特别要求分词,在这种情况下set - $ output既安全又更惯用。另见http://porkmail.org/era/unix/award.html#echo
#1
1
The first grep
is reading standard input. Unless you invoke this with a redirection (please show us!) that's why it appears to hang -- it's waiting for you to type in some input.
第一个grep是读取标准输入。除非你通过重定向调用它(请告诉我们!)这就是为什么它似乎挂起 - 它等着你输入一些输入。
As a quick fix, change
作为快速解决方案,改变
grep INFO: | grep Memory $LOG | awk ...
to read
grep INFO: "$LOG" | grep Memory | awk ...
(The double quotes are not strictly necessary here, but a good idea to learn for the future.)
(双引号在这里并不是绝对必要的,但是为了将来学习是一个好主意。)
Much better, refactor the multiple grep
s and the tail
into the awk
script:
好多了,将多个greps和尾部重构为awk脚本:
awk '/INFO:/ && /Memory/ { g[NR%3+1] = $0 }
END { for (j=1; j<=3; ++j) print g[j] }' "$LOG"
If the order of the output is significant, you have to fiddle it a bit more.
如果输出的顺序很重要,你必须更多地调整它。
Also, new=$output
is vastly preferable over new=$(echo $output)
unless you specifically require word splitting, in which case set -- $output
is both safer and more idiomatic. See also http://porkmail.org/era/unix/award.html#echo
另外,new = $ output比new = $(echo $ output)更受欢迎,除非你特别要求分词,在这种情况下set - $ output既安全又更惯用。另见http://porkmail.org/era/unix/award.html#echo