【问题标题】:Top command in init.d scriptinit.d 脚本中的顶级命令
【发布时间】:2014-06-26 13:26:26
【问题描述】:

我有一个 init.d 脚本和一个 bash 文件。 init.d 脚本执行我的 bash。问题是当 bash 使用 top 命令时,变量 uso 它始终为空。当我只执行脚本时,脚本工作正常。操作系统是 CentOS。

bash 脚本处理进程 mysqld。当进程超过 minimo 值时,脚本会写入日志。

这是我的 bash 代码

#!/bin/bash
echo "Inicio" >> /path/of/file.log
minimo=1
while true
do
    uso=$(top -n 1 | grep mysqld | awk '{ print $10 }')
    if [ -n "$uso" ]
    then
        echo "El uso es:    $uso" >> /path/of/file.log
        if [ $( awk -v uso=$uso -v minimo=$minimo 'BEGIN { if(uso>minimo) print 1; else print 0}' ) -eq 1 ]
        then
            echo $(date)" ------ "$uso >> /path/of/file.log
            sleep 5s
        fi
    fi
done

【问题讨论】:

    标签: bash shell centos init.d


    【解决方案1】:

    使用-b 选项以批处理模式运行top。它通常需要访问终端,而 init.d 脚本不在交互式会话中。

    uso=$(top -b -n 1 | awk '/mysqld/ { print $10 }')
    

    【讨论】:

      猜你喜欢
      • 2012-10-28
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 2013-09-29
      • 2011-04-05
      • 1970-01-01
      • 2016-10-16
      • 1970-01-01
      相关资源
      最近更新 更多