bash - init.d 脚本中的顶级命令

标签 bash shell centos init.d

我有一个 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

最佳答案

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

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

关于bash - init.d 脚本中的顶级命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23553878/

相关文章:

bash - 如何关闭 OS X bash 时间码中的冒号

iphone - 编辑应用程序的 .plist 文件

bash - 将变量从一个 shell 脚本导出到 bash 中的另一个脚本

linux - 如何发现机器类型?

linux - Bash 算术表达式 (( 在复合语句 [[ 返回真?

ruby - 没有 sudo 命令的 Jekyll 安装错误

email - CentOs 邮件服务器限制每 30 分钟

bash - 排序变量

python - 我有一些编号的列表(CSV)。与某些 zip 文件名相同。我只想解压那些特定的 zip 文件

docker - 无法删除 'folder' : Device or resource busy