Bash 脚本运行多次。不知道为什么

Bash 脚本运行多次。不知道为什么

我有一个非常简单的 bash 脚本,它每分钟作为 cron 作业运行一次。它会将日志记录到文件中。一切正常,但有一件奇怪的事情。该脚本同时生成多个日志。我不明白这是怎么可能的。这是代码。

#!/bin/bash


# -c returns number of lines in grep result
isActive=$(systemctl status elasticsearch | grep "active (running)" -c)

if (( $isActive == 0 )) 
then

  systemctl start elasticsearch
  
  timestamp=$(date +"%Y-%m-%d %H-%M-%S")
  touch /root/custom-scripts/elasticsearch/start.log
  echo "${timestamp} Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active." >> /root/custom-scripts/elasticsearch/start.log

fi

cron 任务

* * * * * bash /root/custom-scripts/elasticsearch/start-elasticsearch.sh

日志文件如下所示

2021-07-13 16-17-45 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-17-45 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-17-45 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-17-45 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-20-15 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-20-15 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-20-15 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-23-11 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-26-36 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-26-36 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-26-36 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-33-13 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-33-13 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-33-13 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-33-13 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-33-25 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-34-10 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-35-10 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-40-39 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-40-39 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-40-39 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-40-39 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-40-39 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-41-13 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.
2021-07-13 16-42-07 Elasticsearch service has to be restarted by /root/custom-scripts/elasticsearch/start-elasticsearch.sh because service status was not active.

有人能给我解释一下吗?非常感谢。

答案1

我会检查几件事:

  1. 是否有两个或更多 cron 进程正在运行?您可以使用 ps -ef|grep cron 检查
  2. /etc/cron.d 中是否有任何内容调用此脚本?似乎不太可能,但检查一下也无妨。

相关内容