linux - 当服务器负载高于 20 时运行两个脚本的 Shell 脚本

标签 linux bash shell tomcat

我需要一个脚本,我可以每 5 分钟在 cron 上运行一次,以检查服务器负载是否超过 20,如果是,它将运行两个脚本。

#!/bin/bash
EXECUTE_ON_AVERAGE="15" # if cpu load average for last 60 secs is 
                         # greater or equal to this value, execute script
                         # change it to whatever you want :-)

while true; do
  if [ $(echo "$(uptime | cut -d " " -f 13 | cut -d "," -f 1) >= $EXECUTE_ON_AVERAGE" | bc) = 1 ]; then
    sudo s-
    ./opt/tomcat-latest/shutdown.sh
    ./opt/tomcat-latest/startup.sh
  else
    echo "do nothing"
  fi
  sleep 60
done

然后我 chmod +x 文件。

当我运行它时,我得到了这个:

./script.sh: line 10: ./opt/tomcat-latest/shutdown.sh: No such file or directory
./script.sh: line 11: ./opt/tomcat-latest/startup.sh: No such file or directory

最佳答案

从外观上看,您的脚本正试图将当前工作目录中的两个脚本执行到 opt/tomcat-latest/—— 这不存在。您应该确认两个 shell 脚本的完整文件路径,然后使用它代替当前路径。

另外,我建议您创建一个 cron 来完成这项任务。这是一些关于 crontab 的文档。 https://www.gnu.org/software/mcron/manual/html_node/Crontab-file.html

关于linux - 当服务器负载高于 20 时运行两个脚本的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33131054/

相关文章:

javascript - 如何在 Linux/Mac/Windows 上的 Node/Electron 应用程序中获取浏览器事件选项卡 URL?

linux -/dev/random 权限被拒绝

linux - awk - 根据 4xx 错误代码查找顶部 url

mysql:找不到命令,但它在我的路径中

linux - screen 在 ArchLinux 上找不到文件

linux - 使用 c shell 删除最后两行标准输出?

linux - 如何使用 awk 在 .csv 文件的前两行之后覆盖而不删除它们,并在预告片之前停止覆盖?

php - 在 PHP cli 脚本中更改用户的当前目录

bash - 如何在 Bash 中求幂

shell - 运行带有算术运算的脚本时获取 "numeric argument required"