Hi List
Program Scenario:
Trying to implement a small( may be fake) level application level cluster
implementation.
Directory Structure:
$home/cluster/box1
$home/cluster/box1
$home/cluster/shared_location
>From the box1 and box2:
There are presently two sets program in box1 and box2 1)daemon.pl and
2)clusterservice.pl [ both are same set in box1 and box2 ].
When 1 set of program is running( i.e. from $home/cluster/box1 daemon.pl and
clusterservice.pl they are active ) the other set ( from $home/cluster/box2
daemon.pl is in passive mode i.e. not running and clusterservice.pl is
running).
daemon.pl is running under a while 1 loop.
clusterserservice.pl is checking that daemon.pl is running or not. If not
running the starts the daemon.pl.Now in a cluster scenario when 1 set of
program stops/killed/not running then the other sets start to run.
So how to implement this scenario.
I have written some code but which is not able to handle the whole scenario.
############################# daemon.pl
##############################################################################################
use strict;
use warnings;
use File::Basename;
use Cwd qw(realpath);
my ( $scriptname, $home );
($scriptname, $home ) = File::Basename::fileparse( realpath( $0 ) );
my $sharedloc_info =
"/home/merge/testcode/cluster/sharedbox/daemon_status.txt";
my $progname_abs_path = "$home$scriptname";
my $sharedloc_status =
"/home/merge/testcode/cluster/sharedbox/daemon_execution_status.txt";
my $pid = $$;
my $tm = time_check();
open my $WFH,'>',$sharedloc_info or die "Can't open $sharedloc_info $! \n";
flock($WFH,2);
print $WFH "[$tm] $progname_abs_path $scriptname $pid\n";
close($WFH);
$SIG{INT} = $SIG{TERM} = $SIG{HUP} = $SIG{__DIE__} = sub {
open my $WFH_status,'>>',$sharedloc_status or die "Can't open
$sharedloc_status $! \n";
flock($WFH_status,2);
print $WFH_status "$progname_abs_path\n";
exit 0;
};
while (1) {
#print "Hello I am alive >> /dev/null";
sleep 20;
}
sub time_check {
@_ = localtime(time);
return(sprintf("%02d:%02d:%02d%-02d/%02d/%04d", @_[2,1,0], $_[3],
$_[4]+1, $_[5]+1900));
}
####################################################################################################################################
############################# clusterservice.pl
#########################################################################################
use strict;
use warnings;
use File::Basename;
use Cwd qw(realpath);
my ( $scriptname, $home );
($scriptname, $home ) = File::Basename::fileparse( realpath( $0 ) );
my $continue = 1;
my $sharedloc_info =
"/home/merge/testcode/cluster/sharedbox/daemon_status.txt";
my $daemon_exe = "/home/merge/testcode/cluster/box1/a_tdaemon.pl";
$SIG{TERM} = sub { $continue = 1 };
open my $DATA,'<',$sharedloc_info or die "Cant'open $sharedloc_info $! \n";
flock($DATA,2);
my @watch = [map {split} <$DATA>];
close($DATA);
while ($continue) {
for my $watch (@watch) {
my $status = kill(0,$watch->[3]);
if ( ! $status ) {
sleep 15;
$status = kill(0,$watch->[3]);
if ( ! $status ) {
my $file_name = $watch->[2];
my $proc_status = `ps -ef | grep perl | grep $file_name |
grep -v grep >> /dev/null`;
if ( $proc_status ) {
my $dir_name = dirname($watch->[1]);
print "$watch->[2] is not running from $dir_name\n";
} else {
my $proc_status = `ps -ef | grep perl | grep $file_name
| grep -v grep >> /dev/null`;
if (! $proc_status) {
my $exe_cmd = "perl $daemon_exe";
system($exe_cmd);
print "$scriptname was stopped It has
been started by ","$home \n" ;
}
}
}
}
#check $watch->[0] is a alive
#if not start $watch->[1] and put its pid in $watch->[0]
}
sleep 1;
}
#############################################################################################################################################
With regards
Anirban Adhikary.