Urban pro

View My Profile

Sunday, October 21, 2012

In Linux, Kill many instances of a process with a single command .

Hi guys back to you after a long time.

However, the English that i am typing in here, might be a little murky  as i am typing this one from chennai airport and there's no space to sit on .

Today we are going to look into  a very important question which is :

Suppose you are running thousand or more instances of the VI editor process in your linux. Now you want to kill them in a single shot. How do you do it ?

Let's look at the good old grep-awk :


for pid in $(ps -ef | grep "some search" | awk '{print $2}'); do kill -9 $pid; done
There are ways to make that more efficient,
for pid in $(ps -ef | awk '/some search/ {print $2}'); do kill -9 $pid; done
and other variations, but at the basic level, it's always worked for me.
The problem with this method, however, is that you often end up killing more than you intended. Writing a reliable “some search” is tricky. 

Use Killall :
killall vi
This will kill all command named 'vi'
You might also add a signal as well, e.g SIGKILL
killall -9 vi
The  problem :
  •  Killing by file only works for executables that are kept open during execution, i.e. impure executables can't be killed this way.
  •  Typing killall name may not have the desired effect on non-Linux systems, especially when done by a privileged user.What if i am trying to delete a lot of instances of some non-linux process ?
  •  killall -w doesn't detect if a process disappears and is replaced by a new process with the same PID between scans. If processes change their name, killall may not be able to match them correctly.
But, please be aware that this only works on Linux and BSD. On Solaris and some other systems killalldoes exactly what the name suggests...it kills the init-process.

On AIX, it "cancels all processes that you started, except those producing the killall process."

Use Pkill :

pkill is available on Linux, BSD and Solaris. So it has a bigger spread thankillall. 
But, 

The problem :

pkill doesn't exist on AIX or HP-UX and despite what some people like to believe, there's still a significant based  non-Linux UNIX out in the world.

What does pkill do :

pkill is  available in  (LinuxFreeBSDNetBSDOpenBSDSolaris). You can specify processes by the command name, by the full command line or other criteria. For example,pkill vi kills all programs whose command name contains the substring vi. To kill only processes called vi, use pkill -x vi. To kill only processes called vi with a last argument ending in .conf, use pkill -fx 'vi.*\.conf'.
To see the list of PIDs that pkill would send a signal to, use pgrep, which has exactly the same syntax except that it doesn't accept a signal name or number. To see more information about these processes, run
ps -p "$(pgrep …)"
Under Linux, you need ps -p $(pgrep -d, …) instead (that's a bug: Linux's ps isn't POSIX-compliant).
Another common way to identify processes to kill is the processes that have a certain file open (which can be the process's executable). You can list these with fuser; use fuser -k to send them a signal. For example, fuser -k /usr/bin/find kills all running isntances of find.
If there's a runaway process that keeps forking, you may need to kill the whole process group at once. A process group is identified by the negative of its leader, which is the ancestor process of all the processes in the group. To see the process group that a process belongs to, run ps -o pgid (plus any option to select which process(es) to display). If you determine that you want to kill the process group leader 1234 and all its children, run kill -1234 or kill -HUP -1234 or any other signal.
If you can't find a better way, use ps with proper options to list all processes and filter it with grep or some other text filtering command. Take care not to accidentally match other processes that happen to be running a command with a similar name, or with an argument that contains that name. For example:
kill $(ps -o pid -o comm | awk '$2 == "vi" {print $1}')
Remember that your grep or awk command itself may be listed in the ps output (ps and the filtering command are started in parallel, so whether it will show up or not is dependent on timing). This is particularly important if the command arguments are included in the ps output.
All right that's all guys, i have to get my boarding pass now, or i will miss my flight .

See you next time .



Sunday, October 7, 2012

How to create a modal window ?

All right, so i was working with modal , modal overlay and some other stuffs, when it came to my notice that till now no good tutorial is available for beginners to make them understand the whole process sequentially. So, yours truly took the initiative to write about the process to create a basic modal window .

NOTE : This entire effort has been implemented on a JSP but users can use it anywhere else .

Let me first give you some important information. I was configuring my tomcat 7 with my eclipse indigo, when i found  a lot of problems while trying to figure it out. I had not done this since the college days and it seems few things have changed. People who are interested in doing it can follow this link : http://www.coreservlets.com/Apache-Tomcat-Tutorial/tomcat-7-with-eclipse.html  for configuring eclipse with tomcat 7(This is the best tutorial there is for that at present in www).

What is a dialogue ?

A dialog is a  default built-in behavior , but few methods are needed to control it pro-grammatically,  making this an easy to use widget that is also highly configurable and powerful.

What is a modal window ?

When your dialog box prevents the user from interacting  with the layout of the rest of your web page  if they don't close it manually, then it becomes your modal window.

So, how do we go about making it . Let me first give you the screen snaps for the code i had written :






Beware :

Whenever you are importing jquery.js and jquery-ui.js, remember to import jquery.js before jquery-ui.js, other wise you will get these errors :

jquery -- not recognized
('#id').dialog()  is not a function




Now let us understand the code that matters, shall we !

When you write :

$("#id").dialog(); -> you are actually trying to open a dialogue box and not a modal window, as you can still interact with the rest of the lay out of the page .

So how do i make it a modal window ?

Simple, to do this inside the dialog() method you have to provide --> modal : true. This tells the browser that you want the dialogue to be opened within a modal window to prohibit the user from interacting with the rest of the page.

But nobody will believe me if i don't show what it actually does . Okay, so let's see the screen shots below :

screen 1 : Before clicking on the blue date time link :


screen 2 : After clicking on the blue date time link :





Look at how the rest of the web page has become blurred and any attempt at trying to interact with the rest of the page will be a failure .

That's it for today folks.

I will see you all next time. Good bye .

Friday, October 5, 2012

Why using onload() in a body tag is a bad idea ?

                                         Let's go back to our innocent  years :

Keep Javascript unobtrusive :

To keep our Javascript unobtrusive we can keep it in a single file to avoid messing up the markup of the page. To execute the functions in our .js file, we need to call them when the page has loaded. This can be achieved in various ways and each has it's advantages and disadvantages  .

I am feeling a little nostalgic :

Back when we had just started messing around with Javascript  we used to add the onload to the body element, like this :

body onload = "callMyFunction();"

But, this is bad , real bad .

Why ? --> If we call the script(s) in the body element, we really achieve nothing, since we are still mixing markup and event calls.What we need to do is to separate the call out into a .js file.

I am all grown up :

Now the better way is to attach  the call to the onload event of the window object.

When we only use one function, we don't use the parenthesis at the end of the function name, as that would send back the result of the function rather than trigger the function.

If we have more than one function, we have to use an anonymous function that calls the others, this time with parenthesis.

This is achieved as :


window.onload=callMyFunction; //Notice that there is no ()

or

window.onload=function(){  //Here you need to give () as you are calling an anonymous       // function
 callMyFunction1();
 callMyFunction2();
 callMyFunction3();
}

This will prevent us from messing up our mark up of the page rendered .



Thursday, October 4, 2012

javascript - JQuery TypeError : Type Error

            

                                                               TYPE  ERROR

Most people face this error when they use an outdated jquery.js . Sometimes this error also comes up when someone tries to use jquery in jsp.

The most important thing here that a person must do is first use an upto date version of jquery.js . Don't worry u don't even have to download this stuff.

Just have this :



Now what else can be done, if you still get this error. Well, may be you can replace all the '$' symbols with 'jQuery'. Voila, your application will now start making use of all the jquery api s from its library perfectly.
Hence, instead of :
$('#id').click()
you will have :
jQuery('#id').click()
And everything will fall in place .