Wednesday, 7 March 2012

Running tomcat on specific ip address

Problem:   Want to run tomcat on specific ip address on a host. If your system has multiple ipaddress and want to run tomcat on ip address 10.20.30.40. Follow below procedure

Solution: 

Edit tomcat/conf/server.xml and set ipaddress as value to attribure "address"

 
 
 

Tuesday, 6 March 2012

Unable to install software in ubuntu 11.10

Problem: Unable to install software using Ubuntu Software Center in Ubuntu 11.10. It always throws error "Failed to download package files, Check your Internet connection "

Solution:
Run following command on terminal. This should solve the issue.
 
 sudo apt-get update 

Monday, 27 February 2012

Wednesday, 22 February 2012

Command not found shell script

Problem: Every time I run my shell script "example.sh" it always gives me result "command not found". Though all the commands in script get executed, Still I get this message

Reason: The root cause for this problem is line ending characters represented different in Windows and Linux.

Solution: Convert the file format from Windows to Linux. You can use the command "dos2unix" that will convert line endings, etc from Windows to unix format. i.e. it strips \r (CR) from line endings to change them from \r\n (CR+LF) to \n (LF)
 
# dos2unix example.sh

If you are a java developer and using ant to build your code. You can use below ant task to convert file format from dos to unix.
 

Startup and Shutdown scripts for a java application

Problem:  I have a java application that needs to run as a background process in Linux. The process needs to be started and shutdown with shell scripts.

Approach: We can use nohup utility in linux to run Java process in background. After starting the process write the process id into a file. kill the process during shutdown using process id.

Startup Script:
 
#Start java application as a background process
nohup java -jar app.jar &

#write the process id to file
echo $! > PID



Shutdown Script:
 
#kills the background  java process
kill -9   `cat PID`

#clean up the process ID file
rm -rf PID


 

Monday, 20 February 2012

Shows all the files in a directory and its sub directories

Below java program traverse through directory and displays all the files in it.

 
 
package com.test;

import java.io.File;

/**
 * Shows all the files in a directory and its sub directories 
 * @author luckyacademy
 *
 */
public class ListFiles {
 public static void main(String[] args) {

  showFiles(new File("C:\\dell"));
 }

 /**
  * Recursively shows all the files.
  */
 private static void showFiles(File dirPath) {
  
  for (File file : dirPath.listFiles()) {
   if (file.isDirectory()){ //if it is directory, traverse the directory recursively  
    showFiles(file);
   } 
   System.out.println("file Name: " + file.getAbsolutePath());
  }
 }

}

Running Java process in background



nohup command in linux can be used to run java process in background. 

nohup is most often used to run commands in the background as daemons.

nohup is a POSIX command to ignore the HUP (hangup) signal, enabling the command to keep running after the user who issues the command has logged out. The HUP (hangup) signal is by convention the way a terminal warns depending processes of logout.


  Below command runs the java application in background.
 nohup java -jar sample.jar &