我需要一个脚本,我可以每 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/