Thursday, 5 January 2017

File Permissions in UNIX

rwx(owner user)-rwx(group)-rwx(other)

0 - no permission
1 -> x only
2 -> w only
3 -> wx only
4 -> r only
5 -> rx only
6 -> rw only
7 -> rwx - all


chmod -R 777 to change file permissions.




Wednesday, 4 January 2017

Beauty Of Grep

Usage:grep keyword filename

1) How to ignore some words while doing search using grep in UNIX:

Example:
grep Exception logfile.txt | grep -v Error

2) How to count occurrence of a word in a file using grep command:

grep -c Exception logfile.txt



3) Printing lines after  or before matching a word :

grep -C 6 Exception logfile.txt



4) How to search pattern using egrep and regular expression:

We can use '|' to search for either Error or Exception in single comand

egrep 'Error|Exception' filename.txt



5) -i can be used for case insensitive search

6) zgrep can be used to search patterns in gz files.

7) How to search a whole word in file using grep

grep -w word filename









Tuesday, 3 January 2017

How To Find Out List Of All Open Ports in Linux

Network Statistics(Netstat) can be used to identify all incoming and outgoing connections in a system.

List out all connections: netstat -a




If we want to display only tcp connections, then 't' option can be used to along with option a.
Similarly 'u' option to display UDP connection details.

When viewing the open/listening port and connections,it's often useful to know the process name/pid which has opened that port or connection.

Note: When using p option, netstat must be run with root privileges.

n -> No reverse dns lookup to speed up the output.
l --> Display only listening(established) connections.
t --> Interested only in tcp connections.
p --> Display who opened those connections.
e --> Display user id




Check if a service is running: use grep with pipe

 

Print netstat output continuously (option c)

netstat -cnt | grep ESTABLISHED













FIND Any File or Directory in LINUX

Find command can be used to locate files in LINUX.

Default Usage:

find directory  -name name


Thursday, 29 December 2016

How to Know Free Disk Space

Disk Filesystem(df) command can be used to know free space.

Default Usage: df -h



Happy Learning Day 2 :)


Wednesday, 28 December 2016

How to Find Directory or File Size - Disk Usage

1) Disk Usage(DU) command can be used to know sizes of directories,sub-directories and files.
2) This command works in recursive manner.

Default usage: du -h directory.
   i)   Here h option is for displaying sizes in human readable format.
   ii)  Last line displays, total size of directory.
   iii) This command only considers sub directories by default.


3) Option -a can be used to include files in the directory.
   
 
4) Option -s can be used to know size of directory.

    'S' stands for summary

5) --time can be used for sorting based on modification date.
6) --exclude="*.xml" can be used for excluding specific type of files while computing size of directory.

Happy Learning 😊

Thursday, 2 April 2015

Implementing Shutdown Hook in Java

Graceful shutdown of application is very important aspect to leave our application in consistent state. The primary task of shut downing application is releasing all resources back to system.

We will learn how to bring down, Spring and normal Java apps.

You would have noticed invoking registerShutdownHook() on AbstractApplicationContext in Spring apps, so that Spring gets a chance to clean up it's resources before JVM goes down.

try
{
   AbstractApplicationContext context= new ClasspathXmlApplicationContext("application-config.xml")
   context.registerShutdownHook();
}
finally
{
   context.close();
}

Pls remember, there is no guarantee that always shutdown hook gets executed. for example, if user uses Kill -9 etc hook won't gets it's turn. in cases of, Kill/ctl+c System.exit(0), hook runs before JVM goes off.

This way Spring will cleanup container and invoke destroy methods on beans.

It's also good practice to call close() in finally block. This is because if our application thows unhandled runtime exception, we might of background threads,started by some beans still running and JVM will not terminate.

In case of normal applications, we can register hook with JVM by calling addShutdownHook(Runnable) on Runtime instance.

For example,

public class ShutdownHookExample
{

public static void main(String[] args)
{
ShutdownHookExample shutdownHook = new ShutdownHookExample();
shutdownHook.registerShutDownHook();
}
private Thread shutDownHook = new Thread()
{
  public void run()
  {
  cleanUp();
  }
};

private void registerShutDownHook()
{
//Registering hook with VM
Runtime.getRuntime().addShutdownHook(this.shutDownHook);
while(true);
}

private void cleanUp()
{
System.out.println("cleaning resources");
}
}

Compile an run above program from command prompt and press ctl+c to bring down program and we could see cleaning resources printing in console.

We can close DB connections,File handlers etc in cleanUp method.

Hope you find this post useful.

Happy Learning.