Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have service named "router" that is able to receive http requests.
I decided to write sh script that will periodically check health of router service.
if something is wrong then it will restart it.

The problem:
if router was started with this script, then if this script is terminated, then router service is also terminated.
strange thing is that simple one-line sh script that simply does "service router restart" - works fine (so it restarts the service and closes immediately and everything is fine)

i suppose the reason is somewhere in loop or some bash if things.
script code attached...
Thanks for help!

#!/bin/bash

while :
do
   STATUS=$(curl --silent localhost:80/test)
   if [ "$STATUS" != "OK" ]
   then
    echo "problems: $STATUS".
    service router restart
   fi
   sleep 5
done
share|improve this question
    
Related: (1), (2). –  gronostaj Jun 16 '13 at 18:05
    
That is weird. Try putting a continue after service router restart to see if it works then to try to isolate the problem. service should fork off the process handling and return once the service has restarted, so all I can think of is that you have written the /etc/init.d/router script in a bad way. Post that script as well for anyone to be able to help. –  Daniel Andersson Jun 16 '13 at 18:06
add comment

1 Answer 1

Following is the process of bash script execution:

  1. Bash forks a child, that runs another bash.
  2. This child bash interprets the script, forking child and executing programs as needed.

All programs you run in the script, has this child bash as the controlling terminal.
So, now if the child bash terminates all programs normally terminate.

Since child bash doesn't return until the script exits, you cannot work on parent bash.
So you might need to run the child bash (that means the script) in background. That
lets you continue your work. Background bash doesn't terminate (unless you say or there is some fault).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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