- Linux/Unix Process Management: ps, kill, top, df, free, nice Commands
- Running a Foreground Process
- Running a Background process
- Fg
- Top
- PS
- Kill
- NICE
- DF
- Free
- Summary:
- How to Manage Linux Processes
- What is a Linux Process?
- Types of Linux Processes
- Foreground processes
- Background Processes
- How to List Linux Processes
- How to List the Processes in Tree View
- How to See the Processes of a Particular Program
- How to Kill a Process
- How to List All Processes
- Conclusion
Linux/Unix Process Management: ps, kill, top, df, free, nice Commands
An instance of a program is called a Process. In simple terms, any command that you give to your Linux machine starts a new process.
Having multiple processes for the same program is possible.
- Foreground Processes: They run on the screen and need input from the user. For example Office Programs
- Background Processes: They run in the background and usually do not need user input. For example Antivirus.
Click here if the video is not accessible
Running a Foreground Process
To start a foreground process, you can either run it from the dashboard, or you can run it from the terminal.
When using the Terminal, you will have to wait, until the foreground process runs.
Running a Background process
If you start a foreground program/process from the terminal, then you cannot work on the terminal, till the program is up and running.
Particular, data-intensive tasks take lots of processing power and may even take hours to complete. You do not want your terminal to be held up for such a long time.
To avoid such a situation, you can run the program and send it to the background so that terminal remains available to you. Let’s learn how to do this –
Fg
You can use the command “fg” to continue a program which was stopped and bring it to the foreground.
The simple syntax for this utility is:
- Launch ‘banshee’ music player
- Stop it with the ‘ctrl +z’ command
- Continue it with the ‘fg’ utility.
Let’s look at other important commands to manage processes –
Top
This utility tells the user about all the running processes on the Linux machine.
Press ‘q’ on the keyboard to move out of the process display.
PS
This command stands for ‘Process Status’. It is similar to the “Task Manager” that pop-ups in a Windows Machine when we use Cntrl+Alt+Del. This command is similar to ‘top’ command but the information displayed is different.
To check all the processes running under a user, use the command –
You can also check the process status of a single process, use the syntax –
Kill
This command terminates running processes on a Linux machine.
To use these utilities you need to know the PID (process id) of the process you want to kill
To find the PID of a process simply type
Let us try it with an example.
NICE
Linux can run a lot of processes at a time, which can slow down the speed of some high priority processes and result in poor performance.
To avoid this, you can tell your machine to prioritize processes as per your requirements.
This priority is called Niceness in Linux, and it has a value between -20 to 19. The lower the Niceness index, the higher would be a priority given to that task.
The default value of all the processes is 0.
To start a process with a niceness value other than the default value use the following syntax
nice -n 'Nice value' process name
If there is some process already running on the system, then you can ‘Renice’ its value using syntax.
To change Niceness, you can use the ‘top’ command to determine the PID (process id) and its Nice value. Later use the renice command to change the value.
Let us understand this by an example.
DF
This utility reports the free disk space(Hard Disk) on all the file systems.
If you want the above information in a readable format, then use the command
Free
This command shows the free and used memory (RAM) on the Linux system.
You can use the arguments
free -m to display output in MB
free -g to display output in GB
Summary:
- Any running program or a command given to a Linux system is called a process
- A process could run in foreground or background
- The priority index of a process is called Nice in Linux. Its default value is 0, and it can vary between 20 to -19
- The lower the Niceness index, the higher would be priority given to that task
How to Manage Linux Processes
Arunachalam B
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
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.
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.
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.
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:
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.
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-like structure. It is used as a more visual alternative to the ps command.
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.
To list the child process(es) of a particular process, pass the process id as an argument to the pstree command.
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.
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:
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:
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.
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 :
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.
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
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.