Results 1 to 7 of 7

Thread: Service with respawn, possible?

  1. #1

    Default Service with respawn, possible?

    Hi, everybody!

    I,m trying to reach with goals new service :
    1) On start - run two instances of "socat" with parameters; chmod 777 them;
    2) On stop - kill only those two instances of "socat";
    3) On restart - stop and start service;
    4) On status - check if "socat" instances are running and if not - start them.

    Here is my "service":
    Code:
    #!/bin/bash
        # chkconfig: - 98 02
        # description: socat service
        # processname: socat
    
        KIND="Socat"
        FsIn='/usr/bin/socat PTY,link=/dev/ttyS0,raw,echo=0 tcp4:192.168.0.188:7000'
        ScIn='/usr/bin/socat PTY,link=/dev/ttyS1,raw,echo=0 tcp4:192.168.0.188:7001'
        Port1=/dev/ttyS0
        Port2=/dev/ttyS1
        start() {
                echo -n $"Starting $KIND services: "
                $FsIn &
                $ScIn &
                chmod 777 $Port1
                chmod 777 $Port2
                echo
        }
    
        stop() {
                echo -n $"Shutting down $KIND services: "
                killproc $FsIn
                killproc $ScIn
                echo
        }
    
        restart() {
                    echo -n $"Restarting $KIND services: "
                    killproc $FsIn
                    killproc $ScIn
                    $FsIn &
                    $ScIn &
                    chmod 777 $Port1
                    chmod 777 $Port2
                    echo
        }
    
        status() {
         TEST='ps ax | grep -c $FsIn';
         if [ "$TEST" = "2" ];
         then
            echo "Alive :)"
         else
            echo "Dead :("
         fi
        }
    
        case "$1" in
          start)
                  start
                ;;
          stop)
                  stop
                ;;
          restart)
                  restart
                ;;
    
          status)
                  status
                ;;
          *)
                echo $"Usage: $0 {start|stop|restart|status}"
                exit 1
        esac
        exit $?
    So, "status" section isn't working I have tried a lot of modifications, this is just one of them.
    Can service watch for "socat" (maybe some "while" loop) instances and respawn them if they dies?
    Thanks in advance!

  2. #2
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    32,336
    Blog Entries
    15

    Default Re: Service with respawn, possible?

    Quote Originally Posted by ToLive
    Hi, everybody!

    I,m trying to reach with goals new service :
    1) On start - run two instances of "socat" with parameters; chmod 777
    them;
    2) On stop - kill only those two instances of "socat";
    3) On restart - stop and start service;
    4) On status - check if "socat" instances are running and if not -
    start them.

    Here is my "service":

    Code:
    --------------------

    #!/bin/bash
    # chkconfig: - 98 02
    # description: socat service
    # processname: socat

    KIND="Socat"
    FsIn='/usr/bin/socat PTY,link=/dev/ttyS0,raw,echo=0
    tcp4:192.168.0.188:7000' ScIn='/usr/bin/socat
    PTY,link=/dev/ttyS1,raw,echo=0 tcp4:192.168.0.188:7001' Port1=/dev/ttyS0
    Port2=/dev/ttyS1
    start() {
    echo -n $"Starting $KIND services: "
    $FsIn &
    $ScIn &
    chmod 777 $Port1
    chmod 777 $Port2
    echo
    }

    stop() {
    echo -n $"Shutting down $KIND services: "
    killproc $FsIn
    killproc $ScIn
    echo
    }

    restart() {
    echo -n $"Restarting $KIND services: "
    killproc $FsIn
    killproc $ScIn
    $FsIn &
    $ScIn &
    chmod 777 $Port1
    chmod 777 $Port2
    echo
    }

    status() {
    TEST='ps ax | grep -c $FsIn';
    if [ "$TEST" = "2" ];
    then
    echo "Alive "
    else
    echo "Dead "
    fi
    }

    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart)
    restart
    ;;

    status)
    status
    ;;
    *)
    echo $"Usage: $0 {start|stop|restart|status}"
    exit 1
    esac
    exit $?


    --------------------

    So, "status" section isn't working I have tried a lot of
    modifications, this is just one of them.
    Can service watch for "socat" (maybe some "while" loop) instances and
    respawn them if they dies?
    Thanks in advance!
    Hi
    If you look at /etc/init.d/skeleton for reference, look at touching a
    pid file in /var/run and getting status from that.

    --
    Cheers Malcolm (Linux Counter #276890)
    SUSE Linux Enterprise Desktop 11 (x86_64) Kernel 2.6.27.29-0.1-default
    up 16:33, 2 users, load average: 0.07, 0.12, 0.28
    GPU GeForce 8600 GTS Silent - CUDA Driver Version: 190.18


  3. #3

    Default Re: Service with respawn, possible?

    Thanks for answer. But still no joy. Here is a part of my script:
    Code:
    start)
                echo -n $"Starting socat services: "
                Temp1=/var/run/socat1.pid
                FsIn='/usr/bin/socat PTY,link=/dev/ttyS0,raw,echo=0 tcp4:192.168.1.105:7000'
                Chk1=$(ps -fe | grep "$FsIn" | head -n1|cut -d" " -f 7)
                $FsIn &
                echo $Chk1 > $Temp1
                
              
     # Remember status and be verbose 
     rc_status -v
     ;;
    I want to save the PID to /var/run/socat1.pid. If I exec it in console line by line, it works well. But when I'll try to exec the script, then nothing wrote to socat1.pid file.

    P.S. Sorry for English, I'm in a hurry

  4. #4
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: Service with respawn, possible?

    Look at using /etc/inittab. Web search for inittab.

  5. #5

    Default Re: Service with respawn, possible?

    I have tried inittab, but in some cases it can't respawn my process. For example, if I unplug network cable from comp and plug in it in 10 seconds. Only "init q" can help me in that case

  6. #6
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: Service with respawn, possible?

    So you write a wrapper that init calls to detect anomalous conditions and exit so that init can restart it. Maybe even create a watchdog process to monitor the work process. Thing is you end up writing your own respawn loop anyway if you write your own SysV init script instead of using inittab.

  7. #7

    Default Re: Service with respawn, possible?

    Thanks for answer. I wrote a new script with infinity loop of "init q" and "sleep 30". In this way all working well by the first look

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •