[ic] interchange.pid

Brian Pribis brian.pribis at gmail.com
Thu Nov 29 12:43:01 EST 2007


> 
> Hey,
> 
> Post it here as the first thing to do. We'll take a look; if it
> is generally usable we can put it in the howto section of the docs.
> 
> Thanks,
> -doc
> 
> 
> _______________________________________________
> interchange-users mailing list
> interchange-users at icdevgroup.org
> http://www.icdevgroup.org/mailman/listinfo/interchange-users


You asked for it! ;)


----------------start perl script -------------

#!/usr/local/bin/perl

##############################################################
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA.

##############################################################
## ic_force_restart is designed to handle situations
## where the interchange restart sequence would fail due to
## differences in the OS architecture.
##
## Currently this script is used on the Sun OS, utilizing
## its version of PS in order to discover the interchange
## process ID, kill it and restart the IC server.
##
## If IC's restart or interchange --restart commands work
## fine for you then DO NOT use this script.
##
## The following section will contain variables you may
## need to change in order to get this to work correctly.
##
## cbm


##############################################################
##  Change the following variables to fit your setup.
##
##


## Kill level to use.  This is really important.
## If you don't understand
## then leave the default value of 0.
## Level = 1 will kill the interchange process if a pid is found but
## the pid lockfile (i.e., interchange.pid) is not found or is empty (or
## there is an error reading it, etc).
## Level = 0 will die in the above cases.
$KILL_LEVEL = 0;

## Path to the IC root directory (note trailing slash)
my $ic_root = '/usr/local/interchange/';
my $ic_pid_file = $ic_root.'etc/interchange.pid';
my $ic_error = $ic_root.'error.log';

my $owner = "interch_owner";

## This is normally interchange. However, Sun's ps formatter, -o fname,
## displays on the first eight characters.  So the default should work.
## You could try running the $ps_cmd value bellow from term to see
## what shows up if you are in doubt as to what to put.
my $ic_comm = "intercha";

## You must change this ps command to reflect what is relevent for
## your server.  I have include two below.

# Sun System
my $ps_cmd = "ps -o pid -o user -o fname  -u $owner|";

# Linux (most/all flavors)
#$ps_cmd = "ps -o pid,user,comm -u $owner|";



##
##
##
#####################################################################
## Leave everthing else along from here on (unless you know
## what you are doing).
##
#####################################################################

my $pid_exists = 1;
my $pid_tmp = 0;
my $pid = 0;

#open(ERR, ">>$ic_error") or
#    warn("Cannot write to error log: $!\n");

open(PID, "<$ic_pid_file") or $pid_exists = 0;

if($pid_exists == 0)
{

     print "No pid file found.\n";

     #Check to see if there is a pid for this command anyway.
     $pid = &get_pid;
     if($pid > 0)
     {
	print "There is a pid for interchange, but no lock file.\n";
	if($KILL_LEVEL == 1)
	{
	    print "You have set the KILL_LEVEL to 1.  I will try killing the 
process anyway (This is not a good idea, but you have been warned).\n";
	    kill_pid($pid);
	
	    system($ic_root."/bin/interchange") == 0
		or die "Could not start IC: $?";
	}
	else
	{
	    print "You have set the KILL_LEVEL to 0.  Failing without touching 
anything.\n";
	    exit(1);
	}
     }
     else
     {
	print "No PID found for interchange.\n";
	print "Starting IC from scratch...\n";
	
	system($ic_root."/bin/interchange") == 0
	    or die "Could not start IC: $?";
     }
}
else
{

     #The file exists, so lets get the pid from it.
     while(<PID>)
     {
	chomp($_);
	if($_ eq ""){next;}

	 $pid_tmp = $_;
	 last;
	
     }


    close(PID);

     if($pid_tmp == 0)
     {
	print "No pid found in the interchange.pid file.  Failing.\n";
	exit(1);
     }
     else
     {
	print ("PID $pid_tmp found\n");
     }


     $pid = &get_pid;
     if($pid > 0)
     {
	    print ("Found $ic_comm with pid of $pid\n");
	    print ("Issuing kill -09  and deleting ic pid file...\n");
	    if(&kill_pid($pid) != 0)
	    {
		print "Failed to kill pid.  Interchange is still running.\n";
		exit(1);
	    }
	
	    #Now delete the pid file.
	    unlink($ic_root."etc/interchange.pid") or
		warn("Could not remove etc/interchange.pid file. IC should be fine 
with that: $! \n");
	
	    system($ic_root."/bin/interchange") == 0
		or die "Could not start IC: $!";
	
	
	}
         else
	{
	    print "PID for interchange not found.  Starting interchange.\n";
	    system($ic_root."/bin/interchange") == 0
		or die "Could not start IC: $!";
	}
     close(PS_F);

     #close(ERR);
}


# Will return 0 if succeeds, otherwise it failed.
sub kill_pid()
{
     my $kpid = shift @_;

     @args = ("kill", "-09", $kpid);
     return system(@args);	
}

# Return the pid based on the user settings at the beginning
# of this script.
sub get_pid
{

     my($pid, $pid_owner, $pid_comm);

     open(PS_F, $ps_cmd) > 0 or
	die("The ps command failed. Check your syntax.  Failing:$!");


     while(<PS_F>)
     {
	($pid, $pid_owner, $pid_comm) = split;
	
	
	#This sub is being called even though
	#a pid from the interchange.pid file has not been found.
	if($pid_tmp == 0)
	{
	    if( $pid_comm eq $ic_comm )
	    {
		return $pid;
	    }
	}elsif( ($pid_comm eq $ic_comm) && ($pid_tmp == $pid) )
	{
	    #Found our pid!
	    return $pid;
	}
     }

     return 0;
}
#cbm




-------------------- end perl script -------------


More information about the interchange-users mailing list