Saturday, June 29, 2013

Script command in linux

Script command is very useful way to record logs of any command fired in shell, it logs anything printed on screen.

Example:
--------

script myfile.txt
Will log all results into the file myfile.txt. This will open a sub-shell and records all information through this session. The script ends when the forked shell exits (user types exit) or when CTRL-D is typed.

Killing child process in Linux

I had requirement to stop one of your stats collection process at 7 AM, the stats collection was running from one .sh file which was running a .sql file to collect stats.so, to kill entire process i had to find child process (here child process is .sql file).

I searched through the internet and found various tricks to do it, but most of them seems to be not supporting in my bash version. it tried it myself ,here is the code which is working for me.


export xpid2=`ps -ef|grep -v grep|grep collect_daily_stats2|awk '{print $2}'`
echo "parent:"$xpid2
export cpid2=`ps -ef|grep -v grep|grep "$xpid2"|awk '{print $2}'|sed -s '1d'`
echo "child:"$cpid2
kill -9 "$cpid2"
echo "daily stats2 stopped"
 mailx -s"Daily stats2 stopped successfully" teradatamanager@cvscaremark.com  < /dev/null
else


any suggestions are welcome.

Thursday, May 23, 2013

find string in file using find command


Here is the syntax to find a word in a file.

find . -type f -exec grep -l "word" {} +

Friday, March 15, 2013

List file name containing perticular text in Linux


In Linux Shell to find a particular text string in all the files from the current directory recursively, use find command
 find . -exec grep -l "World" {} \;
This command searches through all directories from the current directory recursively for the files that contain the string “World”.

Friday, March 8, 2013

How to get Linux version information

There are various ways we can get Linux version information  and commands below are commonly used in various Linux distribution.

1. by simply viewing file /proc/version using cat.


[root@localhost /]# cat /proc/version
Linux version 2.6.18-164.el5 (mockbuild@x86-002.build.bos.redhat.com) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)) #1 SMP Tue Aug 18 15:51:54 EDT 2009


2. by using "uname" command.

[root@localhost /]# uname -a
Linux localhost.localdomain 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i386 GNU/Linux

3. by using "lsb_release" commands.

[root@localhost /]# lsb_release -a
LSB Version:    :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: RedHatEnterpriseServer
Description:    Red Hat Enterprise Linux Server release 5.4 (Tikanga)
Release:        5.4
Codename:       Tikanga

4. by using "dmesg" command (display kernal message buffer) .

[root@localhost /]# dmesg |grep "Linux version"
Linux version 2.6.18-164.el5 (mockbuild@x86-002.build.bos.redhat.com) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)) #1 SMP Tue Aug 18 15:51:54 EDT 2009

this command is very useful to get various configuration information from Linux system.

Removing blank lines from file using sed

We can remove blank lines from a file in shell scripting through various command awk,sed etc. using sed command it is very simple..

sed '/^$/d' myFile > outputfile

'^$' character here represent blank line and 'd' stand for deleting.
Note that sed command do not directly make changes to original file, it just display the output on screen, output can be redirected to new file as shown using '> outputfile'.

same can be achived using grep command as below.

grep -v '^$' myFile > outputfile

Sunday, February 24, 2013

How to remove ^M from file using Vi Editor?

We can Replace ^M from file in Vi Editor by using any search and replace command i.e.  :%s/^M//g  or :1,$s/^M//g

Imp to note :

Here ^ is not the simple keyboard key, you can get the character by pressing Ctrl + V.so to get ^M you have to press "Ctrl + V then immediately M.