gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 19:38:12 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com