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