openSUSE Forums > Programming/Scripting » Service with respawn, possible?

Go Back   openSUSE Forums > Programming/Scripting
Forums FAQ Members List Search Today's Posts Mark Forums Read


Programming/Scripting Questions about programming, bash scripts, perl, php, cron jobs, ruby, python, etc.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 21-Sep-2009, 08:33
Puzzled Penguin
 
Join Date: Aug 2008
Posts: 30
ToLive hasn't been rated much yet
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!
Reply With Quote
  #2 (permalink)  
Old 21-Sep-2009, 08:45
malcolmlewis's Avatar
Global Moderator
 
Join Date: Jun 2008
Location: Podunk
Posts: 4,715
malcolmlewis has great reputationmalcolmlewis has great reputationmalcolmlewis has great reputationmalcolmlewis has great reputationmalcolmlewis has great reputationmalcolmlewis has great reputation
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

Reply With Quote
  #3 (permalink)  
Old 07-Oct-2009, 02:41
Puzzled Penguin
 
Join Date: Aug 2008
Posts: 30
ToLive hasn't been rated much yet
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
Reply With Quote
  #4 (permalink)  
Old 07-Oct-2009, 07:01
Flux Capacitor Penguin
 
Join Date: Jun 2008
Location: GMT+10
Posts: 5,232
ken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud of
Default Re: Service with respawn, possible?

Look at using /etc/inittab. Web search for inittab.
Reply With Quote
  #5 (permalink)  
Old 07-Oct-2009, 08:07
Puzzled Penguin
 
Join Date: Aug 2008
Posts: 30
ToLive hasn't been rated much yet
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
Reply With Quote
  #6 (permalink)  
Old 08-Oct-2009, 19:16
Flux Capacitor Penguin
 
Join Date: Jun 2008
Location: GMT+10
Posts: 5,232
ken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud ofken_yap has a reputation to be proud of
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.
Reply With Quote
  #7 (permalink)  
Old 08-Oct-2009, 19:25
Puzzled Penguin
 
Join Date: Aug 2008
Posts: 30
ToLive hasn't been rated much yet
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
Reply With Quote
Reply

Bookmarks


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

Search Engine Friendly URLs by vBSEO 3.3.0 RC2