How To Monitor Linux Process | Monitoring and Managing Linux Process

Introduction:

When we run a program, those instructions are copied into memory and space is allocated for variables and other stuff required to manage its execution. This running instance of a program is called a process. A process is a program in execution. In simple term, any command that you give to your Linux machine start a new process. And a program is identified by its Process ID (PID) as well as its Parent Processes ID(PPID). A process may be in the foreground, in the background or be suspended.

Foreground Processes:

By default, every process that you start runs in the foreground. It gets its input from the keyboard and sends it output to the scree. They run on the screen and need input from the user. For example, Office Programs. If you start a foreground program or process from the terminal, Then you cannot work on the terminal, till the program is up and running. You can use the command “fg” to continue a program which was stopped and bring it to the foreground.

Background Processes:

A background process runs without being connected to you keyboard. If the background process requires any keyboard input, it  waits. They run in the background and usually do not need user input. For example, Antivirus. If you start background process then other task can be carried out while the original process continue executing. To continue running the suspended command in the background, use the “bg” command.

Daemons Process:

These are special types of background processes. Daemons are server processes that run continuously. Most of the time, they are initialized at system startup and then wait in the background until their service is required. A typical example is the networking daemon, xinetd, which is started in almost every boot procedure. After the system is booted, the network daemon just sits ad waits until a client program, needs to connect.

Example to run foreground process
[root@asim ~]# sleep 10
Example to run background process
[root@asim ~]# sleep 20 &
Press ctrl+z to suspend the fg process and type #bg to put in bg
[root@asim ~]# sleep 1000 [root@asim ~]# bg
To view process running in background
[root@asim ~]# jobs
To restart(fg) a suspended process
[root@asim ~]# fg 2
Press cntl+c to terminate fg job
[root@asim ~]# fg 2 Control + c
Running -R   running or runnable(on run queue)
Sleeping-D uninterruptible sleep (usually IO)
Sleeping-S Interruptible sleep, waiting for an event to complete
Stopped-T stopped, either by a job control signal or  because it is being traced
Zombie-X Dead should never be seen
Zombie -Z Zombie process, terminated but not reaped  by its parent

Top Process Monitoring Command:

Top command is used to monitor processes on Linux, its display processor activity in real-time. The processes are listed out in a list with multiple columns for details like process name, PID, user, CPU usage, memory usage. Apart from the list of processes, the top command also shows brief stats about average system load, CPU usage and RAM usage on the top.

Display process
[root@asim ~]# top
Check the top command version
[root@asim ~]# top -v
Display the process for specific user only
[root@asim ~]# top -u cms
[root@asim ~]# top
Shift+M                     -sort the process list by memory usage
Shift+P                      -sort the process list by CPU usage
Shift+N                      -sort the list by process ID
shift+T                       -sort by the running time
Shift+R                      -reverse the sortig order
Shift+A                      -Split output in multiple panels
[root@asim ~]# top
x                      -Highlight the sorted column with bold text
b                      -highlight sorted column background color
d                      -hanged the refresh interval
c                      -Display the full command path
u                      -View the processes of aspecific user only

PS command:

The PS (Process Status) command on Linux is one of the most basic command for viewing the processes running on the system. The ps command is used to provide information about the currently running processes along with detailed information like user ID, CPU usage, memory usage, command name etc. It does not display data in real time like top commands.

Process and terminal associated with  current user
[root@asim ~]# ps
View full list of processes
[root@asim ~]# ps -ef
View full list of process
[root@asim ~]# ps ax
Detailed information about processes
[root@asim ~]# ps aux
Display process by user
[root@asim ~]# ps -f -u root
Display processes by process ID
[root@asim ~]# ps -f -p 10966
Display tree view
[root@asim ~]# ps axjf

Kill Command:

The Kill command is used primarily to terminate processes. To kill, or terminate a process first find out the PID of the process to be killed, then pass the PID number to the kill command.

To find the pid for specific prog
[root@asim ~]# pidof sleep 30
This command will also display pid  of given program
[root@asim ~]# pgrep sleep
View full list of available signal
[root@asim ~]# kill -l
Gracefully terminated process
[root@asim ~]# kill 10532
Forceful terminate process
[root@asim ~]# kill -s 9 10532
Kill all the process of vim by  killall or pkill
[root@asim ~]# pkill -9 sleep
To kill the vim process
[root@asim ~]# pkill vim

Leave a Reply

Your email address will not be published.