Thursday 25 October 2012

Command to search a string in a file in linux



Below is simple command which searches string all the  files in specified URL

find pathToDir -type f -exec grep -l yourstring {} \;


Where

pathToDir :path of directory where you want search string in files.
yourstring :String you want to search


Also check below links :


Wednesday 24 October 2012

How to kill tomcat process in linux

Find and kill tomcat process in linux with below steps:

Step 1 : Find the process id of the tomcat process:

Use  following commands to find tomcat process:


ps -ef  | grep tomcat  :here try with capital Tomcat as well

if not found with above command use below command :

ps -ef  | grep java

if still not found with above commands use below command which lists down all the processes and  go through each processes one by one to find tomcat process:

ps -ef

In above commands
-e :select all process
-f  : list down all the processes

Note *:Check command man ps for more options.

Step 2: From above steps we will able to find out tomcat process pid. use below command to
 kill the process

kill -9 pid

-9 is special Kill signal, which will kill the process. There is very less chance of failing single with option -9.

Note *:Check command man kill for more options.

Step 3 :execute step 1 to check whether tomcat process is killed or not.



How to kill Process In Linux


Some time we fire stop command on particular process using “service” command but it doesn’t stop. In such case we need to stop the process using ps command.

Follow below steps to kill process:

Step 1 :  Before killing the process we need identify which process to kill. Use below command to find particular process:

ps -ef  | grep java


Note: Here all wunning java processes will be listed. change java to other process name as per needed.

-e : select all  running processes
-f :  list down all the process

Description: ps -ef will get all the running process list which is given to grep commnad which will only show java processes.

Step 2: Identify the process id (pid ) from above command result and use below command to kill the process:

kill pid

if above command does not work.Then use below command

kill -9 pid

-9 is special Kill signal, which will kill the process. There is very less chance of failing single with option -9.

step 3 : verify process is killed or not using step 1

Note*: use command man ps and man kill  for more options for ps and kill command.


E.g.  click to see example for find and kill tomcat process



Also check below links :


Tuesday 16 October 2012

Send Mail In Linux With Attachment and Without Attachment



1) Send Mail Command  In Linux   


Listed below different commands to send mail.

1) Send mail by passing mail body from text file.
$ mailx -s "Email Test subject" you@me.com test@me.com  < message.txt

2) Send mail by passing mail body from echo.
$ echo "this is test body" |  mail -s  "Email Test subject"  you@me.com test@me.com

3) Send mail by passing mail body from shell script variable.
$ echo "$body" | mail -s "Email Test subject" you@me.com test@me.com

If above command is not working check below packages and services functioning:

           Check mail service is installed and running with below commands.
           1) Check  sendmail installed or not:

                   a)  For Fedora,Redhat,Cestos or rpm packages : rpm -q sendmail
                   b)  For Ubuntu,Debian or deb packages           : dpkg -s apache-perl

                    If package sendmail is not installed use following command to install sendmail.     

                   a)  For Fedora,Redhat,Cestos   : yum  install sendmail
                   b)  For Ubuntu,Debian           : apt -get sendmail

              2) Check if sendmail service is running :

                   service sendmail status

                   If service is not running start with following command

                   service sendmail start

              3) If mail service is running and mail is not sent then check the log file at /var/log/maillog.

2) Send Mail Command With Attachment In Linux:


       To send mail with attachment perform following steps:

       1)Install sendmail  with following command:

               a)  For Fedora,Redhat,Cestos   : yum  install sendmail

               b)  For Ubuntu,Debian           :  apt -get sendmail


       2) Install mutt with following command:

               a) For Fedora,Redhat,Cestos: yum  install mutt

               b)  For Ubuntu,Debian           : apt -get mutt


        3) Start sendmail service with command service sendmail start

        4) use following command to send mail with attachment


mutt -s "subject" -a "/usr/MyAttachment.txt" me@you.com < tempBody.txt

3) Send Mail In Crontab

         If user want to execute  command using crontab and send a status of command in a mail use following :

e.g  Execute backup.sh every day at 2 am in the morning and send output of script in a mail.

0  2  * *  *  usr/backup.sh | mail -s  "Email Test subject"  you@me.com test@me.com
 


Also check below links :






Some Useful commands In Linux/Unix



There are few commands which are frequently used and we stuck for those command.
I have listed some of the basic commands used frequently.


1) Search string or word in a file in a specified directory:


find /path/to/dir -type f -exec grep -l yourstring {} \; 


2)

Find files by name

a) locate -i filename :This command searches in database.here -i is ignorecase.(This is Fast)
b) find filename        : It searches the file system.(This is slow)

3) Find process and kill particular process.

a) ps -ef | grep tomcat :
Shows running tomcat process details.
command explanation : -e stands for list all process and f formats output view.Output of ps -ef
is given to grep command which search given string in the given output.

b) kill pid : This command will kill the process. If process still not stopped then use kill -9 pid

command.

4) Check the available space on disk:


  df -h : Outputs total space utilized and free space available. -h stands for human readable 
format e.g. mb,gb etc.

5) Check the size of file or directory in linux


  du -ah path : Display the size of directory. “-a” stands for show file along with size and “h”  

for human readable format like mb,gc etc.

6) How to send mail in Linux


mailx -s "Subject line" me@you.com <message.txt : Before firing this command check  

sendmail  is running with command service sendmail status.

7) Installing software in linux is based on different linux flavours


a)RedHat,Fedora,CentOs : yum install software name  e.g yum install sendmail. yum 

command is for rpm packages
b)Ubuntu : apt -get software name  e.g apt -get sendmail. apt command is used for debian 

packages.

8) How start service in linux


  service start servicename :Starts the tomcat service e.g service start tomcat
 service status servicename : Check status of service.
 service stop servicename : Stops the service.

9) Change permission of file or directory in linux:


Below is good link for understanding file permission. Tead it once so concept will be clear 

forever.
http://www.comptechdoc.org/os/linux/usersguide/linux_ugfilesp.html

10) Cut paste file from one location to other (move file from one dir to other)


     a) mv file.txt directorypath/  : Move file.txt  to given directory path
        b) mv file1.txt directorypath/file1.txt  : Move file.txt to directory path and rename it to

file1.txt

11) Renaming file in linux : same mv command is used for renaming file.


mv file.txt file1.txt : As path file.txt and file1.txt is in the same directory mv utility will 

understand it’s a renaming file not moving.

12) Deleting file and folder in linux:

    rm -f filename : Delete file . -f option tell that don’t prompt message like “are you sure you 
want to delete this file?”.
   rm -rf directorypath : This command delete directory. -r will delete subdirectories as well 
-f is explain above.
  rm -f ./*.log : This command will delete all the files in current directory with extension .log.


Also check below links :