12 - Linux Processes Control

Linux is multi tasking OS and it executes multiple processes simultaneous. One of the best futures of working at the terminal line is that we can easily manipulate this type of activity(processes).

Why is important to control the processes in Linux

Well, as administrator you will have to know and understand the Linux Processes and what types they are so you can manipulate then as you like. By manipulating we reefer to the priority of running processes and termination of that processes.

What is Running Processes ?

Also called 'running instance' or 'program' the process is a program in execution. The processes consists in executing program code, resources such as open files, internal data, an address space, one or more threads of execution and a data section containing global variables. Every process has it's own descriptors that keeps the information used to track the procces in the memory. We will not look deep into the processes since this is a wide area in the Linux OS. We are going to describe and explain how we can see and manipulate the running processes.

How to see/list all the Running Processes ?

The ps command is used to list all the Running Processes.

Syntax

    bash:
    ps [option]

Otions

  • [ -a ] - show information about all processes most frequently requested.
  • [ -A ] - show information for all processes.
  • [ -e ] - show information about every process now running.
  • [ -f ] - show full listing.
Note: this are not all the options. -for all the option of the ps command use "ps man" to list them in at the terminal line.

Examples of using the ps command

List all the running processes: List all the running processes with full format listing: List all the running processes for a specific user: -as you can see the list of running processes for user "wannabedba" is diferent from the "root" user. List all the running processes details for a specific processes: Note The "ps" command will just show us an image of the actual running processes, so this is just a snapshot of the current processes.

How do we list all the current processes in real time

The command used for such task is the top command: It produces an ordered list of running processes selected by user-specified criteria,and updates it periodically. This command is very useful because we can see who is using more system resources.

Another command that we can use to get real time running processes details

The htop command was designed to replace the Unix program top.it uses color to give a more detailed visual and also it provides a more convenient cursor-controlled interface for killing processes. If you do not have it on your OS just install it using the "apt-get" command. -this command is quite complete and it easy to use and understand and more important is free. To get more information about this great Linux tool go to http://htop.sourceforge.net/ and make sure you share this with your friends and colleagues.

Now let's go back to TOP command and see some useful example

The point of this examples is for you to understand what is going on behind the "closed doors".We could do all off the following examples using the HTOP tool witch is more intuitive and easy to use but none the less is less used and tested since is a young tool.

Syntax

    bash:
    top [option]

Examples of top commands

For you to understand the content of the output listed by the "top" command we will explain every line listed on the screen.

First line

The second line

The third line

The forth and fifth lines

As we see in the picture here we have the total memory, used memory ,free memory and cached and buffer memory listed.

The header of the processes information detailes

Great, now that we have described the content of the "top" output, let's see how we can interact with it. We will learn how to see the output of the top command in more forms: After the top command was executed and we have the output just by pressing one of the following keys we will get different outputs:
  • M Sort by memory usage
  • P Sort by CPU usage
  • T Sort by cumulative time
  • z Color display
  • k Kill a process -when using this option a prompt will appear asking for the PID(process ID)
  • q quit

Options for the top command

[-u] - with this option we can monitor the process information by UID(user id).

Example

    bash:top -u

[-p] - with this option you can monitor the information by PID(process ID).

Example

    bash:top -p 3306
- where 3306 is the PID for the process. We also can use it for monitoring more then one process:

Example

    bash: top -p 3306 -p 1234 -p 2131
-just by using the -p option more then once Or even more easy
    bash: top -p 3306 , 1234 , 2131
-this will show the same output.

[-n] - it will update the output this (-n) many times.

Example

    bash: top -n 10
- the top output will be displayed 10 times before the top will exit.

[-d] - it set the interval between output update.

Example

    bash:top -d 3
- this command will tell the top command to update the output every 3 seconds. Killing a process After we have seen how to list and monitor our processes let's see hoe we can kill a process. To do so we will create few processes and we will "play"/"kill" with them. We will use the xload "&" witxh is a program that displays a periodically updating histogram of the system load average. Note - "xload" program is displays a periodically updating histogram of the system load average. -the "&" sign will make the program run in the background. So we are going to create a dummy process just to show you guys how to interact and how to kill a process: You can see in the image that we have 3 processes running and we add a new one by opening a "xload" window just to simulate a new process. See that by using the "ps" command we get a list of processes running on the OS, here we have now 4 processes and one of them is out new "xload" process and if hold an PID(process ID) in our case 1734. In the next step we will eliminate this processe by using the kill command as you can see in the picture. See in this picture that the process we have just killed is gone, no longer is he running.

Killing more then

Now we will see how we can kill one or more processes in one line(with one bullet).See here we have created 3 new processes using our xload command.And we will list them using the ps command. Next step is : kill them all :). Now here is the exit of the kill command, see all the 3 processes are no longer alive(running). Note -if the process won't die/stop use the following option of the kill command:
    bash: kill -9 "PID"
- the [-9] -causes the immediate termination of the process.It's like an immediate shutdown.