Test Programs
Innovations on my way!!! New Posts will be at the top of the page

 

                                                                                                                                                                                                              counter

                                                                                                                                                                     Older Posts : Page [1]

 Continuation of UNIX shell script:-

             We saw how to get the pid of a particular process and kill it. Consider the following: We have three files, try1,try2 and try3(these can be of any extension .java or .sh etc) and we need to kill all the three files. One easy way to do is to have a while loop and kill them. Here is the code to do that:

   PID=$(ps -ef | grep try | awk '{print $2}')
   echo $PID
   count=0
   while [[ $count -lt 3 ]] ; do
            kill -9 ${PID}
            count=$((count+1));done

Here the PID gives me all the pid's of the file starting with the name 'try'. We have three files,so we iterate it for 3 times and kill the process using kill command. -lt stands for 'less than'.

     count=$((count+1)) will increment the value of count by 1.

Problems Faced:

     When i tried this program initially, the program could kill only two process. We wondered why it is not killing the remaining one process. Then my mentor tried the following:

In the unix box, type vi <filename> (ex: vi try1.sh). If you find ^M character at the end of each line, delete that character and give :wq to save and exit from the editor.

vi is an standard editor used in unix.

After doing so, you will see that the program will kill all the 3 process.

Note: If you want to kill for n process, then instead of 3 in the while loop, give $PID and all the above mentioned are done using korn shell(ksh)

 

  UNIX Shell Script:-

 Hi friends,                                                                                              

 Its been since long time i have written some of the findings of mine..

 Here comes a new one.... Its related to UNIX shell script.. (KORN SHELL SCRIPT)

 The topic is How do we get only the pid of one process from another shell script and kill that process?.....

  Now I will explain the situation:                                                Download  Example

  I have two shell script files(try1.sh and test.sh). Try1.sh runs at the background and does not have anything..
  It just calls a java program(Samp.java) where there is an infinite loop.

  I need to get the pid and kill that process from another shell script.. test.sh does that.

  In test.sh,

               ps ux | grep "try1" | awk '{print $2}' tells to get the pid of the script file try1.sh.

  print $2 prints the second attribute/field of the output got from ps. '|' means piping the value.

  I store the value of the above command in a variable called temp and printing it using echo command.

  kill -9 ${temp} will kill the corresponding process.

  Now,there might be a question.. Why do we call a java program inside a shell script which does nothing?

  Answer is:

  Consider we dont call java program inside the .sh file.When we try running the try1.sh in the background using
                          sh try1.sh &
  There is nothing in the program to keep running in the background. so it will terminate the process once u give CTRL +c.

  This is the reason why we add a java program which does nothing so that the shell script keeps running and thus we get the process id.

  How to run:

                    compile the java file.

                    sh try1.sh &

                    press CTRL + c

                    sh test.sh

                                                                                                 Older Posts : Page [1]