0

Observation1

logic.sh

#!/bin/bash
#get system metrics
#do stuff and echo it
echo "put metrics" | nc $ip $port
echo "Metrics $metrics"

run_logic.sh

#!/bin/bash
while true;do
  sh logic.sh >> test.log 2>&1 &
  sleep 60
done

start_logic.sh

#!/bin/bash
case $1 in
  start)
       #start the run_logic.sh
       ;;
   stop)
      #stop the run_logic.sh
      ;;
    *)
     echo "Invalid Option!"
     exit 1
esac
exit0

Observation2

logic.sh

#!bin/bash
while true;do
  #do stuff and echo it
  #get System Metrics and put it
  echo $stuff
  sleep 60
done

start_logic.sh

#!/bin/bash
case $1 in
  start)
       #do some stuff, check already started or not
       sh logic.sh >> test.log 2>&1 &
       ;;
   stop)
      #do some stuff
      #Kill the process
      ;;
    *)
     echo "Invalid Option!"
     exit 1
esac
exit0

Now!, In observation1, the scripts are dying in middle of execution. I checked the logs, but I didn't see any error messages. In observation2, the scripts works fine(99% fine!). So, what is difference observation1 and observation2, why scripts are dying in the first case?

0

In observation 1, you start a shell, executing logic.sh every 60 seconds in the background (&). The used command netcat is supposed to send data to an ip/port, but if there is no-one listening on the remote end (tcp as default), it will exit with error.

2
  • Yes, that's correct. But I think that's not the reason for the scripts dying. Even the nc throwing error, it should be running continuously because of while true – Veerendra Kakumanu Nov 30 '15 at 8:53
  • The sleep 60 will prevent it from running continuously; it will wait a minute between runs. – DopeGhoti Nov 30 '15 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.