http://qs321.pair.com?node_id=857785

pileofrogs has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks!

I'm on CentOS linux and I'm writing a script to start important services if they get turned off for some reason. The services in question are all launched by bash scripts on system start, so I'm going to call those same scripts to start dead services.

My problem is, one of those scripts starts its process and puts it in the background by running a foreground process with a '&' at the end. This seems to be keeping the filehandles open and perl doesn't recognise that the launcher (not the service) has ended. Obviously, I should fix that launcher to be better behaved, but I want my perl script to be able to handle badly written launchers.

I'll try and show an example in case that didn't make sense... I'm starting an imaginary server called "crappyserver".

Here's an example launcher /etc/rc.d/init.d/crappyserver

#! /bin/bash case "$1" in start) /usr/sbin/crappyserver & ;; stop) kill `cat /var/run/crappyserver.pid` ;; esac

And here's my launcher launcher

#! /usr/bin/perl -w use Imagniary::Module; if ( !Imaginary::Module::crappyserver_is_running() ) { open(my $handle, '-|', '/etc/rc.d/init.d/crappyserver start') || die "failure!"; while( my $line = <$handle> ) { print $line; } close($handle); print "Done!\n"; }

My launcher-launcher hangs at the open() call. It does launch crappyserver, but it never prints "Done!"

This has something to do with buffering doesn't it...?

Argh!

Any help is greatly appriciated!

--Pileofrogs

Replies are listed 'Best First'.
Re: Launching a launcher?
by TGI (Parson) on Aug 28, 2010 at 06:33 UTC

    I worked on a similar project for a while, but I found monit and scrapped my code. monit is pretty easy to set up, can do all sorts of smart things like restart a process up to n times, and then call for help. You can set up arbitrary tests for to verify process responses. It does alerts as well as process monitoring and management.

    By selecting the appropriate compilation options, you can even keep monit very lean. For example, I was able to use it on a project targeted for ARM based system with 64 MB RAM and still have plenty of room for a couple of Perl daemon processes.

    In short, monit >= sliced bread.


    TGI says moo

Re: Launching a launcher?
by ikegami (Patriarch) on Aug 28, 2010 at 04:06 UTC

    This has something to do with buffering doesn't it...?

    You read from the pipe until it is closed, which could very well be when the server exits. No, buffering is not an issue.

Re: Launching a launcher?
by JavaFan (Canon) on Aug 28, 2010 at 11:09 UTC
    I'm on CentOS linux and I'm writing a script to start important services if they get turned off for some reason.
    Why?

    This is a solved problem. Just put the appropriate entry in /etc/inittab. It's designed to do this stuff.

Re: Launching a launcher?
by afoken (Chancellor) on Aug 28, 2010 at 12:54 UTC

    Once again, daemontools could help. See also the djb way.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)