Manage Processes in Linux- a Guide on Using ps, kill, and nice

Introduction

When most of us use computers, we run various applications. These applications are run by programs called servers or operating systems. There are many different kinds of servers that work in different ways. One of them is Linux. Linux runs applications in the form of processes.

In Linux, we refer to each application as a ‘process’. The server is capable of managing the low-level aspect of the life cycle of the process. As a user, you may need to interact with the server to manipulate some higher-level aspects. You can communicate with and manage the OS by using various tools. There are many commands that you can use to modify various aspects and functions of the server. Here is an easy to follow tutorial on how to easily set up your Linux-based server on Ubuntu. You can further learn how to install the LAMP Stack (Linux, Apache, MySQL, PHP).

This guide focuses on how you can use ps, kill, and nice to manage processes in Linux.

Viewing Running Processes in Linux

  • top

To begin with, one of the most basic commands you should know is top. This tool helps you visualize which processes are currently running on the system. Here is how it will appear when you run it:

top command output

 

At the top of the result, you can locate the system statistics. This gives you information about things like system load and a number of tasks. In this example, you can determine that there is one running process and 55 idle processes. The idle or sleeping processes are the ones that you are not using currently. This means that they are not occupying the system resources. Lastly, you can see all the running processes near the bottom of the pop up with the usage statistics.

  • htop

Before you use this command, you will have to install it from the repositories by typing and running the following:

Once you have it, you can use it to display similar information that you did with top. The primary difference is that with the htop command, you get a more user-friendly result:

htop command output

As you can see, this result is easy to follow and interpret.

Using ps to List Processes

While you can use the above-mentioned commands to view running processes, they may not always cover all scenarios. Fortunately, we have a stronger and more flexible tool at our disposal. This is the ps command.

Let’s explore what you will see if you run the command as is:

ps command output

In this output, you can see all the processes that are running in relation with the current user and session. However, this does not give you a lot of information.

If you want a more holistic view of all the processes running on the system, you need to use an argument. An argument allows you to see all the processes that all users own regardless of the terminal association. The output also appears in a very user friendly and easy to read format. Here is an example:

ps aux

On the other hand, you can also visualize this information in a tree format. Here, as you will see, the hierarchical relationships become apparent as well:

output in tree format

In this example, you can see how kthreadd is displayed as the parent of the subsequently following processes.

  • What are Process IDs?

As a Linux or Unix user, you should know about process IDs. Also known as PIDs, these are unique identities assigned to each process by the system. These identities are how the server keeps track of each individual process. To know the PID of a given process, you have to use the pgrep command, like so:

When you boot your system, the first process starts running. This process is called init. As a default mechanism, the init process gets the PID of ‘1’. You can check the PID of this process as follows:

The init process has the responsibility to then boot all the other programs or processes. Understandably, the following processes will have increasing or larger PIDs.

  • What are Parent Processes?

Another concept to know about is the parent process. If process A spawns process B, then process A is the parent process of process B. To help distinguish between them, the system assigns parent processes a PPID. You can notice this PPID in the column headers whenever you run any management command such as top, htop, and ps.

  • What are Parent-Child Relationships?

As we already know, parent processes spawn child processes. This creation happens in two steps. The first is fork(). This begins by creating a new address space. It also copies the resources of the parent using copy-on-write so that it is available on the child process as well. The second is exec(). This is responsible for loading and executing an executable in the freshly created address space.

  • What if the Child Process Dies Before the Parent Process?

In case this happens, the child process becomes a zombie. That is, until the parent process collects some information about it or tells a kernel that it does not need the associated information. Once this happens, the resources that the process was using will now be free.

  • What if the Parent Process Dies Before the Child Process?

In this scenario, the system will reassign the child process to another parent process. It may be init or any other process.

Sending Signals to Processes in Linux

You can get a given process to respond to you by sending a signal. Signals help you communicate with the operating system. You can use the signal to make an application terminate, launch, or modify a given behavior or task.

  • Using PID to Send Signals

One of the utilities you can use to send signals in Linux is kill. This command, as suggested by its name, helps you terminate or kill a process:

This utility sends the TERM signal to the process which tells it to terminate the process. The command makes the application perform clean up and exit with ease. In case the program does not exit smoothly upon the TERM signal, you can go directly by bypassing the KILL signal:

This signal does not go to the program. It goes to the operating system kernel. The kernel will directly shut the process down. You can use this when a program is ignoring the signals you are sending it.

In this command, you can also replace the name of the signal with the number associated with it. For example, you can use ‘-15’ in place of ‘-TERM’. Similarly, you can replace ‘-KILL’ by ‘-9’.

  • Using Signals for Various Purposes

You can use signals to do other things apart from killing or terminating programs. For example, one issue you may have is dealing with restarting daemons. Every time a daemon receives a hang up signal or HUP, it will restart in programs like Apache. To override this, you can use the following signal:

This command will make Apache reload its configuration. As a result, it will continue to serve you the relevant content.

If you want to see what signals you can send with the kill utility, use the following command:

list of signals

  • Sending Signals by Name

Traditionally, you would send a signal using the PID of the program. However, you do have the option to send signals by using the regular name of the process. To do this, you can use the pkill command. It works similarly to how the pkill command works. The only difference is that it allows you to use the process name:

This pkill command is the equivalent of the following kill command:

You also have a command for when you want to send a signal to every instance instead of a particular process. The following command will send a TERM signal to all Firefox instances running on the system:

Adjusting Process Priorities

Another thing you can do with Linux commands is to adjust priorities. This means that you can decide which process has priority in your server environment. There may be certain processes that you consider critical. Others may not be as necessary. The system will only execute the latter programs when some resources are left over.

You can control process priority in Linux through the niceness command. This value indicates high priority tasks as less nice and low priority processes as more nice. Think of it this way: high priority processes are less nice because they are hoarding the resources. Low priority tasks are sharing them so they are nicer.

You can see the nice value of a given process when you run the top command. This value is located in the ‘NI’ column. High priority tasks would have nice values ranging between ‘-19/-20’. Low priority processes range between ‘19/20’. You will see something like this:

If you want to run a process with a personally assigned nice value, you simply need to use the nice command:

This command will only work when you are starting the given program. If you want to change the nice value of a program that is already running, you have to use renice:

Conclusion

As you can see, the tools here are quite different from the graphical ones. As a result, they can be difficult to understand for a beginner. This guide will help you familiarize yourself with the commands. More practice will help you learn better and use them more efficiently.

Check out our other resources that can help you manage your Linux servers better, including tutorials on how to configure your Linux server to use SSH key-based authentication, locate files on your Linux VPS system with whereis, which, whatis, readlink and find, and read and set environmental and shell variables on a Linux VPS.

Happy Computing!

About Akshay Nagpal

Big Data Analytics and ML enthusiast.