I have a really simple bash script which runs as cron job every minute. It makes a log to file. Everything works fine but there is one weird thing. The script make more than one logs at the same time. I dont understand how is it possible. Here is the code.
#!/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
The cron job
* * * * * bash /root/custom-scripts/elasticsearch/start-elasticsearch.sh
The log file looks like
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.
Is there somebody who can explain it to me? Thanks a lot.