Batch script to manage openoffice as service

OpenOffice

OpenOffice as service running in back ground is seen very unreliable at times. Sometimes it goes down interrupting other services which use it for there operations. To avoid OpenOffice windows service issue I have created below simple tool to check openoffice service time to time, and restart incase of it goes down automatically and send a mail to administrator before restarting it.

For below custom openoffice service checker tool we need few things as listed below:

Batch Scripts(To check and restart openoffice as service):
OpenOfficeChecker.cmd file

Java codes(To send mail to administrator before restarting the service automatically):
AlertMailer.class
AlertMailer.java

Jar Files:
Mail.jar & Activation.jar to send mail

Property file:
inputs.properties : To put the email address and mail host name to send the alert.

Log file:
OOAutostart.wri – All above creates a log file for the events happened during the auto restart activity.

CONTENTS:

OpenOfficeChecker.cmd – Save below script as OpenOfficeChecker.cmd
———————————————————————

@echo off
echo ------------------------------------------------------------------------------- >>OOAutostart.wri
echo $--          Script start Time:%TIME%, Date:%DATE%                          --$ >>OOAutostart.wri
echo $--          All Activities done by script are logged to OOAutostart.wri    --$ >>OOAutostart.wri
echo ------------------------------------------------------------------------------- >>OOAutostart.wri
::  OpenOfficeChecker.cmd
::  only batch
::  AUTHOR:Ramakanta Sahoo(rks2286(at)gmail(dot)com
:: v.0.1

setlocal
set host=localhost
set port=8100
set LOGFILE_PATH=D:\\OOffice\\OpenOffice
set OO_HOME=soffice.exe

echo %host% >>OOAutostart.wri
echo %port%  >>OOAutostart.wri
echo %OO_HOME% >>OOAutostart.wri
@rem if +%1+==++ goto :_err nohost
@rem if +%2+==++ goto :_err
@rem set host=%1
@rem set port=%2
for /f "tokens=2* delims=[" %%h in ('ping -n 1 %host%^|find /i "["') do for /f "tokens=1 delims=]" %%H in ('echo %%h') do set hostIP=%%H
echo Host IP Address set to %hostIP%  >>OOAutostart.wri
if +%hostIP%+==++ goto :_IPNOTFOUND
set result=CLOSED
echo Initial result set to CLOSED  >>OOAutostart.wri
start /min telnet.exe %hostIP% %port%
echo Trying to start telnet.exe %hostIP% %port%  >>OOAutostart.wri
ping -n 7 localhost >nul 2>&1
@rem for /f "tokens=2" %%i in ('tasklist /nh /fi "imagename eq telnet.exe"')
echo searching for Open Telnet connections to port %hostIP%:%port% >>OOAutostart.wri
for /f "tokens=2" %%i in ('tasklist /nh^| find "telnet.exe"')  do (
   rem echo Port:%port% on %host% OPEN!
   @rem set result=Port:%port% on %host% OPEN!
   set result=OPEN
   taskkill /F /PID %%i /T
   @rem tskill %%p >nul 2>&1
)
echo Final result set to %result% >>OOAutostart.wri
if "%result%"=="CLOSED" goto :_EXE

goto :_END
:_err
echo error: Not enought parameters
echo syntax: OpenOfficeChecker.cmd
goto :_END
:_IPNOTFOUND
echo %host%: host unknown
goto :_END
:_EXE
echo cleaning if any soffice process still runnning in dead mode  >>OOAutostart.wri 
for /f "tokens=2" %%i in ('tasklist /nh^| find "telnet.exe"')  do (taskkill /F /PID %%i /T) >>OOAutostart.wri
for /f "tokens=2" %%i in ('tasklist /nh^| find "soffice.exe"')  do (taskkill /F /PID %%i /T) >>OOAutostart.wri
for /f "tokens=2" %%i in ('tasklist /nh^| find "soffice.bin"')  do (taskkill /F /PID %%i /T) >>OOAutostart.wri
echo Trying to Run start /min %OO_HOME% -invisible -"accept=socket,host=0,port=%port%;urp;" >>OOAutostart.wri
start /min %OO_HOME% -env:RTL_LOGFILE=%LOGFILE_PATH% -invisible -"accept=socket,host=0,port=%port%;urp;"
echo Sending Mail >>OOAutostart.wri
java -classpath ".;./mail.jar;./activation.jar" AlertMailer
:_END

AlertMailer.java: Need to be compiled to AlertMailer.class before use
———————————————————————-

//AUTHOR:Ramakanta Sahoo(rks2286(at)gmail(dot)com
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class AlertMailer {
  public static void main (String args[])
      throws Exception {
   


        ResourceBundle bundle = ResourceBundle.getBundle("inputs");
//        String OutPutLogName = bundle.getString("OutPutLogName").trim();
        String host = bundle.getString("MailServerHostName").trim();
        String from = bundle.getString("MailFrom").trim();
        String to = bundle.getString("MailTo").trim().toLowerCase();
        String cc = bundle.getString("MailCc").trim().toLowerCase();
        String bcc = bundle.getString("MailBcc").trim().toLowerCase();
       // String fileAttachment = bundle.getString("MailAttachmentFile").trim();
        String MailSubject = bundle.getString("MailSubject").trim();
        String MailBody = bundle.getString("MailBody").trim();
        System.out.println("cc:" + cc);

        System.out.println("Bcc:" + bcc);

        if (host.trim().length() == 0) {
            host = "null";
        }
        if (from.trim().length() == 0) {
            from = "null";
        }
        if (to.trim().length() == 0) {
            to = "null";
        }
        if (cc.trim().length() == 0) {
            cc = "null";
        }
        if (bcc.trim().length() == 0) {
            bcc = "null";
        }
//        if (fileAttachment.trim().length() == 0) {
//            fileAttachment = "null";
//        }
        if (MailSubject.trim().length() == 0) {
            MailSubject = "Mail Alert !!";
        }
        if (MailBody.trim().length() == 0) {
            MailBody = "Hello";
        }

        try {
            // Get system properties
            Properties props = System.getProperties();

            // Setup mail server
            if ((host.trim().length() != 0) || (!host.equalsIgnoreCase("null")) || (!host.equalsIgnoreCase(null)) ) {
                props.put("mail.smtp.host", host);
            } else {
                System.out.println("Can not leave MailServerHostName NULL");
            }
            // Get session
            Session session =Session.getInstance(props, null);

            // Define message
            MimeMessage message = new MimeMessage(session);
            if ((from.length() != 0) && !from.equalsIgnoreCase("null") && !from.equalsIgnoreCase(null)) {
                message.setFrom(new InternetAddress(from));
            } else {
                System.out.println("Can not leave MailFrom NULL");
            }
            if ((!to.equalsIgnoreCase("null")) && (to.length() != 0) && !to.equalsIgnoreCase(null)) {
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            } else {
                System.out.println("Can not leave MailTo NULL");
            }
            if ((!cc.equalsIgnoreCase("null")) && (cc.length() != 0) && !cc.equalsIgnoreCase(null)) {
                message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
            }
            if ((!bcc.equalsIgnoreCase("null")) && (bcc.length() != 0) && !bcc.equalsIgnoreCase(null)) {
                message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
            }
            message.setSubject(MailSubject);

            // create the message part
            MimeBodyPart messageBodyPart = new MimeBodyPart();

            //fill message
            messageBodyPart.setText(MailBody);

            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);

            // Part two is attachment
           // messageBodyPart = new MimeBodyPart();
            //DataSource source = new FileDataSource(fileAttachment);
            //messageBodyPart.setDataHandler(new DataHandler(source));
            //messageBodyPart.setFileName(fileAttachment);
            //multipart.addBodyPart(messageBodyPart);

            // Put parts in message
            message.setContent(multipart);

            // Send the message
            Transport.send(message);

        } catch (Exception e) {//Catch exception if any
            System.err.println("Error In Sending Mail: " + e.getMessage());
            System.out.println("Stack Trace:");
            e.printStackTrace();
        }


//End Send Mail Block
  }
}

inputs.properties: Need to filled with proper details for the mailing to work
——————————————————————————-

MailServerHostName=exch-01.fattttyadmin.com
MailFrom=OpenofficeAlerter()
[email protected] -> Put your Admin group or Administrator address
MailCc=null -> If required mention alternate mail addresses or else keep it as it is.
MailBcc=null -> If required mention alternate mail addresses or else keep it as it is.
MailSubject=[OOCheker Alert]Regarding OpenOffice Status !!
MailBody=Hi,\n\nOpenOffice Service was down.\n\nOpenOffice Service checker started the service.\n\nRegards,\nOOAlerter

mail.jar and activation.jar files can be downloaded from internet. Its freely available.

OOAutostart.wri – Example Log File for activities performed during the auto restart for future reference
———————————————————————————————————-

------------------------------------------------------------------------------- 
$--          Script start Time:22:07:00.07, Date:Thu 07/23/2010             --$ 
$--          All Activities done by script are logged to OOAutostart.wri    --$ 
------------------------------------------------------------------------------- 
localhost 
8100  
soffice.exe 
Host IP Address set to 127.0.0.1  
Initial result set to CLOSED  
Trying to start telnet.exe 127.0.0.1 8100  
searching for Open Telnet connections to port 127.0.0.1:8100 
Final result set to OPEN 

Put all above files into a single folder in OpenOffice service machine and schedule the batch script to run each 5minutes or so. Do remember to check the port number to something different from the default 8100 if you are using some other port.

Benefits:
You dont need to have softwares to check service which may cost you.
Reduces the manual service restarts from time to time.
Saves time and updates you about the service down times also.

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

4 Responses

  1. Paicamync says:

    Hello 🙂

  2. LAKSHMI NARAYANAN says:

    Hi,

    Do you have Batch script and Java code to manage OpenOffice service availability for Linux. Kindly share me.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.