DEV Community

Cover image for How to manage processes in Linux?
5 Mins Learn
5 Mins Learn

Posted on • Originally published at gogosoon.com

How to manage processes in Linux?

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
Enter fullscreen mode Exit fullscreen mode

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.

sleep 10 &
Enter fullscreen mode Exit fullscreen mode

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.

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.

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

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
Enter fullscreen mode Exit fullscreen mode

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.

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

pstree 3149
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

a=$(pidof chrome) kill $a
Enter fullscreen mode Exit fullscreen mode

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.

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
Enter fullscreen mode Exit fullscreen mode

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

top -u gogosoon
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

After installing just put the name in terminal

gnome-system-monitor
Enter fullscreen mode Exit fullscreen mode

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

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

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

Have a look at my site which has a consolidated list of all my blogs.

Top comments (0)