Monday, May 23, 2011

Zombie Process Understanding.

A zombie process is a process that has completed execution but still has an entry in the process table, allowing the process that started it to read its exit status.

When a process ends, all of the memory and resources associated with it are deallocated so they can be used by other processes.

However, the process's entry in the process table remains. The parent is sent a SIGCHLD signal indicating that a child has died; the handler for this signal will typically execute the wait system call, which reads the exit status and removes the zombie.

The zombie's process ID and entry in the process table can then be reused. However, if a parent ignores the SIGCHLD, the zombie will be left in the process table.

In some situations this may be desirable, for example if the parent creates another child process it ensures that it will not be allocated the same process ID.


If you have zombie processes it means those zombies have not been waited for by their parent.

To remove zombies from a system, the SIGCHLD signal can be sent to the parent manually, using the kill command. If the parent process still refuses to reap the zombie, the next step would be to remove the parent process. When a process loses its parent, init becomes its new parent. Init periodically executes the wait system call to reap any zombies with init as parent.


How to find "zombie" process in Linux ?

Execute "top" command & read top left corner to check zombie process. If you are unable to identify the process, press "z" & you will get "red" colored identification for easy understanding.

How to kill "zombie" process in Linux ?

Run this command.
ps aux | awk '{ print $8 " " $2 }' | grep -w Z


Output would be,
Z 3456
Z 2107
Z 1708
Use "Kill command" for all three processes as given below.

kill -9 3456
kill -9 2107
kill -9 1708

That's it. Enjoy Linux.


Tuesday, May 3, 2011

Commands to check "Disk Usage" in Linux

'du' = Finding the "disk usage"

du

Typing the above at the prompt gives you a list of directories that exist in the current directory along with their sizes. The last line of the output gives you the total size of the current directory including its subdirectories. The size given includes the sizes of the files and the directories that exist in the current directory as well as all of its subdirectories. Note that by default the sizes given are in kilobytes.

du /home/nishith
The above command would give you the directory size of the directory /home/nishith


du -h
This command gives you a better output than the default one. The option '-h' stands for human readable format. So the sizes of the files / directories are this time suffixed with a 'k' if its kilobytes and 'M' if its Megabytes and 'G' if its Gigabytes.

du -ah
This command would display in its output, not only the directories but also all the files that are present in the current directory. Note that 'du' always counts all files and directories while giving the final size in the last line. But the '-a' displays the filenames along with the directory names in the output. '-h' is once again human readable format.

du -c
This gives you a grand total as the last line of the output. So if your directory occupies 100MB the last 2 lines of the output would be

100M .
100M total


The first line would be the default last line of the 'du' output indicating the total size of the directory and another line displaying the same size, followed by the string 'total'. This is helpful in case you this command along with the grep command to only display the final total size of a directory as shown below.

du -ch | grep total
This would have only one line in its output that displays the total size of the current directory including all the subdirectories.

du -s
This displays a summary of the directory size. It is the simplest way to know the total size of the current directory.

du -S
This would display the size of the current directory excluding the size of the subdirectories that exist within that directory. So it basically shows you the total size of all the files that exist in the current directory.

du --exclude=mp3
The above command would display the size of the current directory along with all its subdirectories, but it would exclude all the files having the given pattern present in their file names. Thus in the above case if there happens to be any mp3 files within the current directory or any of its subdirectories, their size would not be included while calculating the total directory size.


'df' = Finding the "disk free" space

df

Typing the above, outputs a table consisting of 6 columns. All the columns are very easy to understand. Remember that the 'Size', 'Used' and 'Avail' columns use kilobytes as the unit. The 'Use%' column shows the usage as a percentage which is also very useful.

df -h
Displays the same output as the previous command but the '-h' indicates human readable format. Hence instead of kilobytes as the unit the output would have 'M' for Megabytes and 'G' for Gigabytes.
Example :

I have my Linux installed on /dev/hda1 and I have mounted my Windows partitions as well (by default every time Linux boots). So 'df' by default shows me the disk usage of my Linux as well as Windows partitions. And I am only interested in the disk usage of the Linux partitions. This is what I use :

$ df -h | grep /dev/sda1 | cut -c 41-43

This command displays the following on my machine

78%

Please Note: You can find your drive letter by typing "fdisk -l / df -kh" command line.



Thanks,
Nishith N.Vyas