Tuesday, April 26, 2016

Find and kill a process in one line using bash

For operation and other pusposes, we need to kill process in linux system. For this purpose, at first we find the process id then we kill the process with the process id. i.e

[agentrpt@localhost ~]$ ps -ef | grep 'jar'
agentrpt 13131     1  0 16:49 ?        00:00:00 java -jar ABS_ALERT_MIDDLEWARE.jar
agentrpt 13278     1  0 16:54 ?        00:00:00 java -jar SMSMailClient.jar
agentrpt 13402 13367  0 16:58 pts/0    00:00:00 grep jar

For killing process of java -jar SMSMailClient.jar, We need to executge command as:
kill 13278

We can do it in a simple way. For the using the script regulary we can create a script with the below command.

In bash, you should be able to do:

kill $(ps aux | grep '[SMS]MailClient.jar' | awk '{print $2}')
Details on its workings are as follows:

The ps gives you the list of all the processes.
The grep filters that based on your search string, [SMS] is a trick to stop you picking up the actual grep process itself.
The awk just gives you the second field of each line, which is the PID.

Thursday, April 21, 2016

Remove all files/directories except for one file

find . ! -name 'filetoexist.txt' -type f -exec rm -f {} +

will remove all files except filetoexist.txt. To remove directories, change -type f to -type d and add -r option to rm.

To exclude list of files: ! \( -name one_file -o -name two_file \)

In bash, to use rm !(file.txt), we will have to enable extglob:

$ shopt -s extglob
$ rm !(file.txt)

Note that extglob only works in bash and Korn shell family. And using rm !(file.txt) can cause an Argument list too long error.

In zsh, you can use ^ to negate pattern with extendedglob enabled:

$ setopt extendedglob
$ rm ^file.txt

or using the same syntax with ksh and bash with options ksh_glob and no_bare_glob_qual enabled.

Wednesday, April 20, 2016

To get IP Address of Sun Server

For a normal user (i.e., not 'root') ifconfig isn't in his path, but it's the command.
More specifically: /usr/sbin/ifconfig -a
If we want the IP Address easily, we need some scripting as:
/usr/sbin/ifconfig -a | awk 'BEGIN { count=0; } { if ( $1 ~ /inet/ ) { count++; if( count==2 ) { print $2; } } }'

Monday, April 18, 2016

Modifying file using Vim and awk

In some case, we have to change a big file with the same type of text. We can use vim editor for doing this type of task. Suppose, we have a files as below:


Now for a need, we need to generate delete script from this file contents as below:


For doing so, 

1. we have opened the file using vim editor. vim loglistws.txt
2.Then we have used the below commnads to add double quate around the words : 
   :%s/EJB_Str/"EJB_Str/g
   
Then save the file using :w
   :%s/\.log/\.log"/g

3. Now save the file using :wq!
4. Now we have made a new file form the existing file as:
    cat loglistws.txt | awk '{print "rm ",$9,$10,$11,$12}' > myscriptws.sh

Sunday, April 17, 2016

Comparing between two packages

Many times we need to compare between different packages, wars, jars to find out the specific differences. We can use different tools for this purpose i.e pkgdiff, japi-complicance-checker, clirr. Here the process of finding differences has been depicted using the pkgdiff.

1. Download the zipped file from: https://github.com/lvc/pkgdiff/archive/1.7.2.tar.gz
2. Untar the files using the command: tar xvfz somefilename.tar.gz
3. Open the unzip folder.  in this folder make script is available.
4. Install the file using the command:  sudo make install prefix=/usr
5. Now compare the files using:  pkgdiff filename.jar filename_new.jar
6. The above command will provide the result.
7. A report will also be generated in html. We can get the details report from that file.