Email

在 cron 中縮小的頂部輸出

  • June 17, 2016

我正在嘗試通過 cron 發送電子郵件以了解哪些程序佔用了我的伺服器。它工作正常,但生成的電子郵件太窄了,我需要了解更多有關該過程的資訊。

這是我正在使用的腳本(從這裡偷來的:http: //www.inmotionhosting.com/support/website/server-usage/create-server-load-monitoring-bash-script):

#!/bin/bash
trigger=10.00
load=`cat /proc/loadavg | awk '{print $1}'`
response=`echo | awk -v T=$trigger -v L=$load 'BEGIN{if ( L > T){ print "greater"}}'`

if [[ $response = "greater" ]]
then
top -cSbn 1 | head -14 | tail -8 | mail -s"High load on server - [ $load ]" myemail@domain.com
fi

生成的電子郵件類似於:

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND

810809 root      20   0 20700  13m 3032 R   50  0.0   0:00.40 /prod/process-nam
810802 root      20   0 20700  13m 3032 R   48  0.0   0:00.39 /prod/process-nam
810808 root      20   0 20708  13m 3032 S   48  0.0   0:00.35 /prod/process-nam
810803 root      20   0 20708  13m 3032 S   46  0.0   0:00.39 /prod/process-nam
810810 root      20   0 20168  13m 3028 R   46  0.0   0:00.33 /prod/process-nam
318723 www-data  20   0  146m 131m 3320 R   45  0.4  67:27.96 /home/server/pr
810800 root      20   0 20704  13m 3032 S   45  0.0   0:00.39 /prod/process-nam

如您所見,整條路徑都失去了。但是,如果我從 bash 執行腳本,它就可以工作,發送足夠寬的電子郵件。

這是一個ncurses問題嗎?管道郵件問題?

謝謝!

謝謝斯文,但我的頂級輸出是這樣的:

# top -w
top: unknown argument 'w'

我猜你有一個更新的版本。但是您向我發送了正確的方向(為您+1):我更改了腳本以導出列上的數字,現在我有了完整的輸出:

#!/bin/bash

trigger=15.00

load=`cat /proc/loadavg | awk '{print $1}'`

response=`echo | awk -v T=$trigger -v L=$load 'BEGIN{if ( L > T){ print "greater"}}'`

if [[ $response = "greater" ]]
then
#sar -q | mail -s"High load on server - [ $load ]" recipient@YourDomain.com
export COLUMNS=512
top -cSbn 1 | head -14 | tail -14 | mail -s"High load on server YODA - [ $load ]" quinhentos@queo.pt
fi

使用-w開關top

  -w  :Output-width-override as:  -w [ number ]
       In  'Batch'  mode,  when  used without an argument top
       will format output using the COLUMNS= and LINES= envi‐
       ronment  variables,  if set.  Otherwise, width will be
       fixed at the maximum 512 columns.  With  an  argument,
       output width can be decreased or increased (up to 512)
       but the number of rows is considered unlimited.

       In normal display mode, when used without an  argument
       top  will  attempt to format output using the COLUMNS=
       and LINES= environment variables,  if  set.   With  an
       argument,  output  width  can  only  be decreased, not
       increased.  Whether using environment variables or  an
       argument with -w, when not in 'Batch' mode actual ter‐
       minal dimensions can never be exceeded.

       Note: Without the use  of  this  command-line  option,
       output  width is always based on the terminal at which
       top was invoked whether or not in 'Batch' mode.

引用自:https://serverfault.com/questions/721738