How to manage processes in Linux?
Tags: linux, beginners, processess
Published: 2023-03-06 12:31:44

Everyone in the world, be it a Professional, or a Student, will follow a certain process to achieve their goal. Similarly, every system has its own process to accomplish a task. Every program/command that executes in a Linux system is called a process.

In this blog, let’s explore processes and their management in Linux.

## What is the process?

A process is theoretically called a program in execution. It’s basically a task that a system is currently working on. Every action one makes on the system will result in a new process. For example, Opening a Browser initiates a process.

In simple words, a process is an instance of a program. The user action is transformed into a command and a new process will be created upon the execution of the command.

Processes work in Parent-Child Hierarchy. As the name of the hierarchy implies, a process initiated from a command/program is called the parent process and the produced process of a parent process is called the child process.

## Types of process

Processes are classified into 2 types in Linux Distributions.

 1. Foreground Process

 2. Background Process

## Foreground Process

A process that requires the user to start them using the Terminal command or Program is called the foreground process. This means the foreground process requires an input trigger from a user. So every foreground process is manually triggered.

Whenever a process is running in the foreground, the other process should wait until the current process completes.

The best example to demonstrate this is via sleep command. sleep command does not allow the user to interact with the terminal until the given number of seconds is completed.

    sleep 10

![](https://cdn-images-1.medium.com/max/2000/0*vTYyUYMqbo18ptb8)

We should wait for 10 seconds to access the terminal to run another command.

## Background Process

A process that runs independently on user input is called a background process.

Unlike the foreground process, we can run multiple processes at the same time in a background process. To run a process in the background, place an ampersand (&) at the end of the command that we use to start the process.

Here’s a quick example to demonstrate that

Let’s execute the sleep command in a background process. It'll run in the background and gives the terminal back to us to run other commands.

![](https://cdn-images-1.medium.com/max/2000/0*k52fgAHU6dk6ix3p)

    sleep 10 &

Now we can see that the above command runs in the background, It created a process with the PID ( 19003 ). So we can run another command simultaneously ( pwd command).

## How to change the foreground process to the background?

If we start a process in the foreground and would like to place it in the background, We can do it using bg command. Let's see how to change the foreground process to the background.

If a process is running, press the key CTRL+Z . This command will suspend the current process.

![](https://cdn-images-1.medium.com/max/2096/0*ybUQhHNc5BKvEium.png)

Then run the bg command. It takes process id as an argument and places the process into the background, If the argument is empty it will place the currently suspended process in the background.

![](https://cdn-images-1.medium.com/max/2000/0*zmTQ-uqWQa3XRqh1.png)

## How to list the processes?

Before knowing about this, we should know “Why do we need to know the list of processes?”. The answer to my question mostly falls under one of the below categories.

 1. To know which process consumes more time.

 2. To know which process takes more memory and CPU usage.

 3. To know the triggered command for a running process.

To know the processes that are running currently, We can use ps ( Process Status ) command.

    ps

![](https://cdn-images-1.medium.com/max/2000/0*yaKmla7PfltA5he6)

To list the summary of all processes of every logged In user we can use the command. This command is the combination of who, uptime and ps -a commands in Linux.

    w

![](https://cdn-images-1.medium.com/max/3002/0*saJVQezedbVawvx4)

## How to list the processes in Tree View?

When a program/command runs, It initiates a main process called the parent process. The parent process may depend on some other command/program which will create a child process.

Here’s an example screenshot.

![](https://cdn-images-1.medium.com/max/2000/0*SToOp91ZKfOpeeVZ)

In the above screenshot, Firefox is the parent process and the other processes are its child processes.

Let’s explore how to list the process in a tree-like structure.

pstree is a Linux command to list the currently running process of all users in a tree manner. It is used as a more visual alternative to the ps command.

    pstree

![](https://cdn-images-1.medium.com/max/2000/0*iTaZte1JimkJPAag)

As we can see the running processes are in a tree manner. It’ll be useful to visualize the processes.

Adding -p flag with the command will display each branch with it's process id.

![](https://cdn-images-1.medium.com/max/2000/0*saPeCen6zyMHdS3q)

To list the child process of a particular process pass the process id as an argument to pstree command.

    pstree 3149

![](https://cdn-images-1.medium.com/max/2000/0*y6dKBdtpu8loErD-)

Earlier, I mentioned that pstree command lists the processes from all the users. Passing the username along with pstree commands lists only the processes run by the user.

    pstree root

![](https://cdn-images-1.medium.com/max/2000/0*7r7OrBNzkC_AIGx7)

The above screenshot shows the processes running by the root user.

## How to see the process of a particular program?

Most developers would have faced the following scenario.

While working on web development projects, we use browsers like Chrome, Firefox, and a few more to verify the output with different browsers. Few developers will keep on opening the tabs and never close the opened one. Due to heavy load (if 150+ tabs are opened) browsers will not respond at some times 😣 leading to the system hanging up. The worst part could be that we won’t be able to close the browser 😂.

Unlike Windows, we don’t have TaskManager in Linux to kill the browser.

This problem can be solved easily in Linux too. Let’s see how a Linux expert handles this scenario.

He knows that every program (including the browser) runs as a process. Then just find the process id and kill it.

Let’s see how to find the process id of a command/program you need.

In my system, chrome is running, Now we can get the PIDs of chrome by running the following command.

    pidof chrome

![](https://cdn-images-1.medium.com/max/2000/0*Nlib_t1fWs-s1OCY)

## How to Kill a Process?

There is a command called kill in Linux that is used to kill any process, by passing the PID ( Process id ) or Process Name.

Here’s the syntax of kill the command

Let’s store the pid of chrome and kill it using the kill command.

![](https://cdn-images-1.medium.com/max/2000/0*Swn4PF1KviAChHgt)

    a=$(pidof chrome) kill $a

The above command will kill the chrome web browser.

## How to list all the processes?

We can see all the Linux processes using the **top **command. It shows real-time updates of each process for all users.

![](https://cdn-images-1.medium.com/max/2000/0*5_FsUiI0x4QKvSev)

Let’s understand the heading, to understand the underlying data.

*PID* represents a Unique process ID

*USER* represents the Username of the owner of the task

*PR* represents the Priority of the process. The lower the number higher the priority

*NI* represents a Nice Value of task. A Negative nice value implies higher priority, and a positive Nice value means lower priority.

*VIRT* represents the total virtual memory used by the task.

*RES* represents RAM Usage of a process in kilobytes

*SHR* represents Shared Memory Size ( Kb ) used by a process

*S* represents the Status of the process

*CPU* represents the CPU usage

*MEM* represents the memory usage of task

*TIME* represents the CPU Time

*COMMAND* represents the command that used to start the process

To display specific user processes we should use the flag -u

    top -u

To look at the processes run by the user gogosoon, run the following command

    top -u gogosoon

![](https://cdn-images-1.medium.com/max/2000/0*0KQLVVJAprdxNzTh)

I hope you’re confused about seeing the command line output 😆.

It’ll be a bit hard to debug the processes in real time.

Here comes another UI tool to handle the processes in Linux. But we have to install this manually.

    sudo apt install gnome-system-monitor

After installing just put the name in terminal

    gnome-system-monitor

This will open all the processes in a new window with a decent UI.

![](https://cdn-images-1.medium.com/max/2330/0*AQBJ9Rha5unvmwQr)

When we right-click on any process it will show the actions like kill, stop, end, etc.

The Resources tab shows the following utilities.

 1. CPU History

 2. Memory and Swap History

 3. Network History

![](https://cdn-images-1.medium.com/max/3200/0*JFVbxu7fRtdDeabs)

![](https://cdn-images-1.medium.com/max/3200/0*hGBKbeBla1z4c3uC)

![](https://cdn-images-1.medium.com/max/3200/0*Lqx-Fq_3FOtlJgIE)

These graphs will be useful to determine the load in our system.

## Conclusion

In this article, you have learned the processes in Linux. Hope you all got some idea about processes in Linux. I recommend you all try these commands in your system.

To read more such interesting blogs, subscribe to my email newsletter on my [site](https://5minslearn.gogosoon.com/?ref=devTo_process_in_linux)

Have a look at my [site](https://5minslearn.gogosoon.com/?ref=devTo_process_in_linux) which has a consolidated list of all my blogs.