简体   繁体   English

当服务器负载超过20时运行两个脚本的Shell脚本

[英]Shell script to run two scripts when server load is above 20

I need a script that I can run on a cron every 5 minutes that will check if server load is above 20 and if it is it will run two scripts. 我需要一个可以每5分钟在cron上运行一次的脚本,该脚本将检查服务器负载是否大于20,如果负载超过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

I then chmod +x the file. 然后,我chmod + x该文件。

When I run it I get this: 当我运行它时,我得到以下信息:

./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

From the looks of it, your script is trying to execute the two scripts from the current working directory into opt/tomcat-latest/ -- which doesn't exist. 从外观上看,您的脚本试图将两个脚本从当前工作目录执行到opt / tomcat-latest /中-该脚本不存在。 You should confirm the full file paths for the two shell scripts and then use that instead of the current path. 您应该确认两个Shell脚本的完整文件路径,然后使用它代替当前路径。

Also, I'd recommend that you create a cron to do this task. 另外,我建议您创建一个cron来执行此任务。 Here's some documentation about the crontab. 这是有关crontab的一些文档。 https://www.gnu.org/software/mcron/manual/html_node/Crontab-file.html https://www.gnu.org/software/mcron/manual/html_node/Crontab-file.html

check the permission to execute the files shutdown.sh and startup.sh 检查执行文件shutdown.sh和startup.sh的权限

Is sudo -s not sudo s- 是sudo -s不是sudo s-

And I recommend to put a sleep (seconds) 我建议睡一会儿(秒)

sudo -s /opt/tomcat-latest/shutdown.sh
sleep 15
sudo -s /opt/tomcat-latest/startup.sh

Or better 或更好

sudo -s /opt/tomcat-latest/shutdown.sh && sudo -s /opt/tomcat-latest/startup.sh

The startup.sh will executed only if shutdown.sh was executed with success. 仅当成功执行shutdown.sh时,startup.sh才会执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在启动时运行两个Shell脚本? - How to run two shell scripts at startup? 是否可以编写一个接受输入的 shell 脚本,然后将 ssh 连接到服务器并在我的本地机器上运行脚本? - Is it possible to write a shell script that takes input then will ssh into a server and run scripts on my local machine? Shell:如何并行运行两个脚本A和B并在A结束时停止B - Shell: how to run two scripts A and B in parallel and stop B when A is over 如何在脚本中在另一台服务器上运行shell脚本? - How to run shell script in another server in a script? 如何在不同服务器上的另一个 shell 脚本中运行 shell 脚本? - How to run a shell script within another shell script on a different server? 运行两个shell脚本,首先需要用户输入 - run two shell scripts where first requires user input 如何通过Shell脚本在远程服务器上连接和运行Shell脚本 - How to connect and run shell script on remote server, via shell script 并行运行两个shell脚本并捕获它们的输出 - Run two shell script in parallel and capture their output 编写Shell脚本时如何使命令同时运行? - how to make command run concurrently when programming shell scripts? 如何使用另一个 shell 脚本在单个 go 中一个接一个地运行多个 shell 脚本 - how to run multiple shell scripts one by one in single go using another shell script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM