perl script to monitor & restart listener hangs on nohup waiting for carriage return

StackOverflow https://stackoverflow.com/questions/17579607

  •  02-06-2022
  •  | 
  •  

문제

I have a tcp/ip listener written in java that is run for multiple 'sites' each with their own port. The developer who wrote this listener did so as a standalone so I have to manage each via a start_comtrol.sh script which is quite tedious. I'm new to perl, but I wrote a quick script today to check each pidfile compare to make sure the process is running and attempt to restart if not as shown below.

use strict;
use warnings;

my @sites = qw / FOO BAR FOO2 /;

foreach (@sites) {
    my $pidfile = "/usr/local/sbin/listener/$_/pid.file";
    my $start_comtrol = "/usr/local/sbin/listener/$_/start_comtrol.sh";
    open my $file, '<', $pidfile or die 'Could not open file:  ' . $!;
    my $pid = do { local $/; <$file> };
    close $file;
    my $exists = kill 0, $pid;
        if ( $exists ) {
            print "The running process for $_ is $pid\n"; #temp print to screen for debugging
            # Do Nothing
        }
        else {
            exec $start_comtrol;
    }
}

Each start_comtrol.sh script is identical in it's contents:

#!/bin/ksh
export CLASSPATH=.:ojdbc6.jar:Base.jar:ojdbc14.jar:log4j.jar
#export CLASSPATH=/home/aspira/controller/ojdbc6.jar:/home/aspira/controller/Base.jar:/home/aspira/controller/ojdbc14.jar
nohup java com.aspira.comtrol.listener.BaseListener  &
echo "$!" > pid.file

The script runs just fine when the process is found running, however if the process isn't running and it attempts to start it via the exec $start_comtrol.sh it encounters nohup waiting for carriage return and doesn't move on to the next variable in the sites array.

The running process for FOO is 19401

The running process for BAR is 1228

[root@isildur]# nohup: appending output to `nohup.out'

What's the best way to handle this so it doesn't hang up on the irrelevant prompt from nohup?

도움이 되었습니까?

해결책

You're looking for the system() function, not exec()

From the doc for exec():

The exec function executes a system command and never returns; use system instead of exec if you want it to return.

Try doing system( $start_comtrol ) instead.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top