[英]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.