Batch file status based On PID(process id) example

Click here to download eclipse supported ZIP file





This is TestBatch.bat JSP file and it is used as the input for the application.




@echo off
tasklist /FI  "PID eq 8924" /NH /v > TEST_BATCH.log
exit






This is BatchFileStatus.java file having the source code to execute business logic.



 

    
package com.cv.java.batch;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

/**
 @author cvkodam
 *
 */
public class BatchFileStatus {

  /**
   *
   */
  public BatchFileStatus() {
  }

  /**
   @param args
   */
  public static void main(String[] args) {

    System.out.println("Control entered into main(-) method!!!");
    final String batchFileName = "TestBatch.bat";
    String bFile = ClassLoader.getSystemResource(batchFileName).getPath();
    if (bFile != null) {
      if (bFile.startsWith("/")) {
        bFile = bFile.replaceAll("/""//");
        bFile = bFile.substring(2, bFile.lastIndexOf("//")+2);
      }
    }
    String splitTarget = bFile;
    String[] split = splitTarget.split("target");
    String firstDir = split[0];
    final String file = bFile;
    final String fileName = firstDir;
    Timer timer = new Timer();
    TimerTask hourlyTask = new TimerTask() {
      @Override
      public void run() {
        try {
          Runtime.getRuntime().exec("cmd /c start " + file+batchFileName);
          String logFileName = fileName+"TEST_BATCH.log";
          boolean flag = isFileExist(logFileName);
          if (flag) {
            BufferedReader br = new BufferedReader(new FileReader(logFileName));
            readLogFile(br);
          }

        catch (Exception ioException) {
          System.out.println(ioException.getMessage());
        }
      }


    };

    // schedule the task to run starting now and then every hour...
    timer.schedule(hourlyTask, 0l1000 01 60);

  }

  private static boolean isFileExist(String propFileName) {

    File file = new File(propFileName);

    if (file.exists()) {
      return true;
    }

    return false;
  }

  public static void readLogFile(BufferedReader br) {

    readContentInsideFile(br);

  }

  private static void readContentInsideFile(BufferedReader br) {

    // Open the file
    try {
      String line = null;

      boolean flag = false;
      // Read File Line By Line
      while ((line = br.readLine()) != null) {
        // Print the content on the LOGGER
        if (line != null && line.length() 1) {
          if (line.contains("Running")||line.contains("Console")) {
            flag = true;
            // System.out.println("line is : : " + line);
          }
        }
      }
      if (!flag) {
        System.out.println("Batch file is down...");
      else {
        System.out.println("Batch file is running...");
      }
    catch (FileNotFoundException e) {
      e.printStackTrace();
    catch (IOException e) {
      e.printStackTrace();
    finally {
      // Close the input stream
      try {
        if (br != null)
          br.close();
      catch (IOException e) {
        e.printStackTrace();
      }
    }

  }

}


This is pom.xml file having the entries of dependency jars and information to build the application .



	
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cv.java.batch</groupId> <artifactId>BatchFileStatusBasedOnPID</artifactId> <version>1.0</version> </project>


This is log4j.properties file having the entries for logging the information into the console/file.




#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n




No comments:

Post a Comment