Commands for Process Management in Linux

Published on November 4, 2024

In Linux, a process refers to a program in execution. Whenever you run a Linux command, you create a  process and, depending on the type of the created process, it can run independently or require an input to process.

As a Linux user, it’s essential to understand what processes are and the different commands that you can use to manage the processes in their different states. For instance, how would you check all the running processes? What about terminating a running process? To answer these questions, that’s where knowing the different commands for process management in Linux comes into play.

Types of Linux Processes

Before we see the different commands you can use for process management in Linux, let’s begin with understanding the different types of processes you will encounter or create.

There are two main types of processes:

1. Foreground process – They are the processes initiated by the user and they take input from the user and will display the output on the terminal. With these processes, the terminal is not usable until the process completes execution or gets interrupted. They are referenced as interactive processes.

2. Background process – They are non-interactive processes that can be initiated by the user of the system. These processes have a unique PID and other processes or commands can be executed on the terminal as they run without interrupting them.

Now that we know the available types of Linux processes, let’s quickly go through the different states that a foreground or background process can be in.

Below ate the different process states in Linux:

1. Running – A process that is actively running or ready to start running is said to be in a running state.

2. Sleeping – It is a state for all the processes that are waiting for a resource to be available for them to take the next step.

3. Stopped – It is a state for all the process that received a stop signal.

4. Zombie – These are the processes that still have their entry in the process table yet they are dead.

Three Commands for Process Management in Linux

So far, you have understood what a process is, the types of Linux processes available, and the different states that a process can take. This section will guide you on the various commands that you can use to manage different processes in Linux. We will focus on the three main commands that lets you view, control, and interact with processes in Linux. Take a look!

1. The Linux top command

Withe the Linux top command, you will interact with processes in real-time. It is a handy command for seeing all the running process in real-time. The command displays different outputs. For instance, the PID is the processes ID associated with the process. The User is the username of the owner of the process. The PR is the priority of the process and the NI is the priority value of that process.

In the output, the S represents the state of the particular process. Here, you will see states such as D(uninterruptible sleep), R(running), S(sleeping), T(stopped), and Z(zombie). You will also see other metrics such as the percentage of CPU used by a process (%CPU) and the total CPU time the process is using (TIME+).

Run the top command as follows:

Copy

To quit the process, press ‘q’. You can also kill a process by pressing ‘k’ then add the process ID.

2. The Linux ps Command

The ps command also displays the currently running process but unlike with the top command, the ps command doesn’t show the output in real-time.

Here’s it’s basic usage:

Copy

To view all the running processes on your Linux, add the -A flag as follows.

Copy

With the ps command, adding the -u will display more information about a process.

Note that, with the ps command, there are several flags that you can supply it to get more results. It all depends on what details you want to get for different process. Feel free to check its manual page for more details.

3. The Linux kill Command

Apart from viewing the processes on your Linux, you also need a way of terminating them, and that’s where the Linux kill command is handy. The command offers different signals that you specify to interrupt and terminate a process.

To view all the supported signals, run the below command.

Copy

Before killing a process, you need its process ID. You can fetch it using different methods. An example is using the top command with the grep command like in the example below.

Copy

Once you have the process ID, kill it by specifying its PID.

Copy

That’s how you kill a process in Linux.

4. The Linux nice Command

The last command involves changing the priority of a process. When you run the top command, the NI is the niceness value of a process and it ranges from -20 to 19.

Assuming you want to open Firefox and set its niceness value to 5, you would run the below command.

Copy

When you run the top command, you will notice that the NI value for the target process is what you specified when opening it.

For a running process, you can also update its niceness value by specifying the new value and the PID of that process. Using Firefox, with PID=6026, we would use the below command.

Copy

Recheck the NI value using the top command to verify that our update has worked as expected.

Conclusion

This post covers four commands you can use to manage processes in Linux. We’ve discussed the top, ps, kill, and nice commands. Go through each trying them out to get comfortable working with them. With that, you will get comfortable working with processes in Linux.

New to LinuxMeta? Get Started Now! 

Instantly Deploy Linux & Windows KVM VPS at a Cheap Price