Wednesday, October 24, 2012

Create new file depending on the last modified time



The following script would automatically create .trg files if a file doesn’t exist. It also checks for the file last modified time. Here I have placed it as 5 minutes. Only if the file has not been modified for last 5 minutes it goes and creates the .trg file. The timing can be extended as needed. This should prevent the script from creating .trg files for the files that are getting processed. 


#!/bin/bash

cd /hulanas/smartdoc/inbound

for file in *.xml
do
# check if .trg file already exists, if yes ignore
  if [ -a $file.trg ]
    then
       echo "$file.trg already exists"
# check if the file has not been modified for more than 5minutes (time can be increased depending on the requirement)
# if yes, go ahead and create the .trg file.
# This is to prevent .trg files from being created for the files that 
# might be getting processed.
  elif [ `find $file -type f -mmin +5` ]
     then
# create the .trg file
        touch $file.trg
  fi
done

Friday, September 7, 2012

Reading portion of a huge log file

If you have a several hundred MB log file, how do you quickly find a portion of it you're interested in? Your vi may not be able to open the file, complaining file too large, and/or take too long and use too much memory. Here's what you can do.

Let's say you want Mon Jul 15's data from July's access_log, here's the command
sed -n '/^Mon Jul 15/p; /^Tue Jul 16/q' access_log > /tmp/qq 
Without the quit command, sed would scan the file to the end or till you press ^C. Note the undocumented semicolon that allows you to put two sed commands on one line. (By the way, on Solaris creating a file under /tmp may be faster because tmpfs should be memory-based, unless you're short on RAM. But remember to delete big files under /tmp when you're done because they reduce available swap space.)

Saturday, April 28, 2012

OutOfMemoryError vs StackOverflowError

OutOfMemoryError:
Thrown when Java is running out of memory and cannot allocate any more memory in the heap for the object

StackOverflowError:
The stack space lives at the top-end of the address space and the heap lives at the bottom-end of the address space. As stack grows, it grows downwards. As heap grows, it grows upwards. So there is a potential that the two can collide and when that happens, you get a StackOverflowError thrown. Typically happens when you call some methods recursively (that keeps going-and-going).


Sunday, April 15, 2012

Command Line Arguments


packageorg.training.fundementals;

public class Args {

      /**
       * @param args
       */
      public static void main(String[] args) {
     
            System.out.println(args[0]);
            System.out.println(args[1]);
            System.out.println(args[2]);
            System.out.println(args[3]);
System.out.println(args[3]);
System.out.println(args[3]);

           

      }

}

C:\> java Args This is a simple Java program
Output:
This
is
a
simple
Java
Program


Saturday, February 25, 2012

Running Multiple Instances of Tomcat on Windows Machine

To run multiple instances of tomcat the two most important things that you need are CATALINA_HOME and CATALINA_BASE variables set properly.

CATALINA_HOME refers to the base tomcat directory which has all the files required for starting and stopping tomcat. It mainly includes two folders bin and lib.

CATALINA_HOME  would be common for all the tomcat instances running on that machine.

CATALINA_BASE would be different for different instances and each instance would refer to its particular CATALINA_BASE directory. This usually includes the following folders inside it, conf, webapps, temp, work & log.

Inside the conf folder, edit the server.xml file to listen to different ports for different instances.

How to have multiple CATALINA_BASE variables ?

Download and copy the tomcat folder to some directory of your choice. I have installed it like this F:\Servers\apache-tomcat-7.0.26

Now create another folder and inside that create a folder name bin. Inside bin create two files namely startup.bat and shutdown.bat

I created a folder name Elephant (which is one of the name of my tomcat instance) under F:\Servers\

Now in the startup.bat put the following lines and edit the path as appropriate.


set CATALINA_BASE=F:\Servers\Elephant
set CATALINA_HOME=F:\Servers\apache-tomcat-7.0.26
F:\Servers\apache-tomcat-7.0.26\bin\startup.bat

In shutdown.bat put the following:


set CATALINA_BASE=F:\Servers\Elephant
set CATALINA_HOME=F:\Servers\apache-tomcat-7.0.26
F:\Servers\apache-tomcat-7.0.26\bin\shutdown.bat