We all follow certain processes to achieve our goals. Similarly, every system has its own processes to accomplish tasks.
Every program or command that executes in a Linux system is called a process.
In this tutorial, let's explore processes and how we can manage them in Linux.
What is a Linux Process?
A process is theoretically called a program in execution. It's basically a task that a system is currently working on.
Every action you take 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 according to a 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 Linux Processes
Processes are classified into 2 types in Linux Distributions:
- Foreground Processes
- Background Processes
Foreground processes
A process that requires the user to start it using a Terminal command or Program is called a foreground process. This means that foreground processes require an input trigger from a user. So every foreground process is manually triggered.
Whenever a process is running in the foreground, the other processes should wait until the current process completes.
The best example to demonstrate this is via the sleep
command. The sleep
command does not allow the user to interact with the terminal until a given number of seconds has passed.
sleep 10
sleep
terminal command running on foreground and blocks user input
We should wait for 10 seconds to access the terminal to run another command.
Background Processes
A process that runs independently on user input is called a background process. Unlike the foreground processes, 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 you 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.
Sample Terminal command for a background process
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 a Foreground Process to a Background Process
If we start a process in the foreground and would like to place it in the background, we can do it using the 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.
Foreground process output
Then run the bg
command. It takes a 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.
bg <process_id>
bg
Foreground process to background process output
We can see the suspended command ( sudo apt update
) resuming in background.
How to List Linux Processes
Before we go over how to do this, you should know why you might need to know a list of processes. Here are a few reasons:
- To know which process consumes more time.
- To know which process takes more memory and CPU usage.
- To know the triggered command for a running process.
To see the processes that are running currently, we can use ps
(Process Status) command:
ps
ps
command showing the list of running processes
To list the summary of all processes of every logged-in user, we can use the w
command. This command is the combination of the who
, uptime
and ps -a
commands in Linux.
w
w
command displaying the list of processes of all users
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.
Child processes of the parent (firefox) process
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-like structure. It is used as a more visual alternative to the ps
command.
pstree
pstree
command listing processes in tree view
As we can see, the running processes are in tree form. This can be useful to visualize the processes.
Adding the -p
flag with the command will display each branch with its process id.
pstree -p
Terminal command displaying the list of processes in tree view with PID
To list the child process(es) of a particular process, pass the process id as an argument to the pstree
command.
pstree 3149
Listing processes in tree view for a particular process
Earlier, I mentioned that pstree
command lists the processes from all the users. Passing the username along with the pstree
command lists only the processes run by the user.
pstree root
Listing processes in tree view for a particular user
The above screenshot shows the processes running by the root
user.
How to See the Processes of a Particular Program
Many developers may have faced the following scenario:
While working on web development projects, we use browsers like Chrome, Firefox, and others to verify the output with different browsers.
Some developers will keep on opening the tabs and never close the opened ones. Due to heavy load (if 150+ tabs are opened) browsers will not respond sometimes 😣 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 Task Manager 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.
We know that every program (including the browser) runs as a process. So then you just have to 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
Terminal command to find process id of chrome
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 the kill
command:
kill <pid/processname>
Let’s store the PID of Chrome and kill it using the kill command:
a=$(pidof chrome)
kill $a
Terminal command to kill a process
The above command will kill the Chrome web browser.
How to List All Processes
We can see all the Linux processes using the top
command. It shows real-time updates of each process for all users.
top
Terminal command displaying all the process in real-time
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. 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:
- D: Uninterruptible sleep
- R: Running
- S: Sleeping
- T: Traced (stopped)
- Z: Zombie
- 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 <username>
To look at the processes run by the user gogosoon
, run the following command:
top -u gogosoon
Terminal output of all process started by user gogosoon
You might be confused about seeing the command line output 😆. It'll be a bit hard to debug the processes in real time.
Here comes the handy GUI tool to handle the processes in Linux. But we have to install this manually. This will work more like task manager in Windows.
sudo apt install gnome-system-monitor
After installing, just type the name of the software in terminal:
gnome-system-monitor
This will open all the processes in a new window with a decent GUI:
Gnome-System-Monitor
When we right-click on any process it will show the actions like kill, stop, end, and so on.
The Resources tab shows the following utilities:
- CPU History
- Memory and Swap History
- Network History
CPU History graph
Memory and Swap History Graph
Network History Graph
These graphs will be useful to determine the load in your system.
Conclusion
In this article, you have learned the basics of processes in Linux. I hope you now understand how they work a bit better. I recommend you all try these commands in your system.
To learn more about Linux, subscribe to my email newsletter at my site and follow me on social media.