Documentation Archive Developer
Search

ADC Home > Reference Library > Technical Q&As > Tools > WebObjects >

Automatically starting and stopping application instances without using Monitor


Q: Monitor is difficult to wrangle on my platform. Is there another way to automatically start and stop application instances? See figure 1 for an example script.

A: Yes, many sites use specialized scripts to control their application instances. A popular implementation language is PERL.



#!/usr/local/bin/perl
$x=0;
# $processPath defines the application's process path
$processPath="/this/is/the/path/to/the/application/foo.woa";
# $processName defines the application's name
$processName="foo";
# logLocation defines the absolute path to the log file
$logLocation="/this/is/the/path/to/the/log/logfile.log";
# logDirectory defines the path to the log file(s)
$logDirectory="/this/is/the/path/to/the/log/";
#smtpHost is the value of the WOSMTPHost startup variable
$smtpHost = "foo.bar.net";
# basePortNum is the first port number your instances will live under
$basePortNum = 4321;
#maxinstances is the maximum number of instances your application will have
$maxinstances=1;
@prreturn='';
@value='';
#    Open the LogFile
printlog("-------------------------------------------------------------------");
printlog("Killing all the $processName processes");
#    Finds all #processName jobs & kills them
@prreturn=`ps -aef | grep $processName`;
foreach $i (0 .. $#prreturn){
  @value=split(" ",$prreturn[$i]);
  if ($value[$#value-1] ne "grep"
          && $value[$value-1] ne $0 && $value[$value-1]){
    system "kill -9 $value[1] >> $logloc 2>&1";
    printlog("Killed process $value[1]");
   }
}
# a script to preserve the old log instead of overwriting with a new one would be
# appropriate here
#    Start the Application
printlog("Starting $processName");
foreach $instance (0 .. $maxinstances-1)
{
$portnum = $basePortNum + $instance;
system "$processPath $processName -WOPort $portnum -WOSMTPHost $smtpHost \
    -WOCachingEnabled YES -WOListeningQueueSize 10 \
    -NSJavaMinHeapSize 33554432 -NSJavaMaxHeapSize 67108864 $processName  \
    >>$logDirectory/$processName.$portnum 2>&1 &";
}
#    Subroutine to write to log file
sub printlog {
    open(logfile,">>$logLocation");
        &date_print;
    print logfile "@_\n";
    close (logfile);
    }
sub date_print
{
        $currtime = time();
            @mytime = localtime($currtime);
            $yyyy =  $mytime[5];
            if ($yyyy > 70)
             { $yyyy += 1900 ;}
            else
             {$yyyy +=2000; }
            printf(logfile "%04d/%02d/%02d %02d:%02d:%02d ",
             $yyyy,$mytime[4]+1,$mytime[3],$mytime[2],
             $mytime[1],$mytime[0]) ;
}

Listing 1. A source code listing showing some code that helps to explain my idea.




[Jul 25 2001]