linux - service started from sh script stops when sh script

07
2014-07
  • rshmelev

    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
    
  • Answers
  • mohit

    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).


  • Related Question

    boot - Controling when services start in Fedora 14 GNU/Linux
  • Gentleman Ryan

    Whenever I start up my laptop running Fedora 14, I notice that it says that it is failing to connect to my default wireless network since the wireless card is not activated. Since it appears that the wireless card is not activated until the NetworkManager service is started, is there anyway for me to set NetworkManager to start before this check is made? Boot message log is attached.

    http://pastebin.com/810yT456


  • Related Answers
  • ianweller

    The "Device does not seem to be present" message appears to be coming from the network service, which is different from the NetworkManager service (it uses older-style /etc/sysconfig files).

    Set your system to only start the NetworkManager service. You can do this in system-config-services, or by running the following as root:

    # /sbin/chkconfig network off
    

    And remember that this won't disable NetworkManager, so you'll still be able to connect.