Score:3

Bash script runs more than one times. Dont know why

in flag

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.

hr flag
Unrelated to the problem, but systemctl has an `is-active` action that you can test directly ex. `if systemctl --quiet is-active elasticsearch; then ...`
in flag
Thanks I did not know about it.
hr flag
The only thing I can think of that might explain what you're seeing is if the elasticsearch.service file has an `ExecStartPre` or `ExecStartPost` that (directly or indirectly) invokes the same script
in flag
Dont understand it. Elasticsearch does not know about the script.
in flag
The reason is I did not heart about Restart=on-failure and the second is the log file which shows me the fail cases.
in flag
Uf did not heart.... Sorry Mrs. English.
in flag
Dont know about systemd.timer. I would like to know why this happened with standard cron job. I am not an Linux expert. I need to manage it on "my level". Which should work.
ng flag
Add `$$` to your message to show the PID of the process, for debugging
Score:0
cn flag

I would check a couple of things:

  1. Are there two or more cron processes running? You can check with ps -ef|grep cron
  2. Is there anything in /etc/cron.d that is calling this script? Seems unlikely, but won't hurt to check.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.