Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!


while :
   STATUS=$(curl --silent localhost:80/test)
   if [ "$STATUS" != "OK" ]
    echo "problems: $STATUS".
    service router restart
   sleep 5
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

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

Your Answer


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.