More than 10 examples of Linux killing processes are explained in detail

In this tutorial, we’ll talk about killing processes in Linux with several examples. In most cases, it’s as simple as typing the “kill” command followed by the process ID (often abbreviated as PID).

As shown in the Linux kill process usage example above, we killed a process with ID 1813.

How does Linux kill processes? If you’re a Windows user, it might be helpful to think of the “kill” command as the “end task” button in Windows Task Manager in Linux.

Lists the processes that are running

The “ps -e” command will list everything running on the system. Even with minimal installation, the command may output more than 80 results, so it’s much easier to pipe the command to “grep” or “more”.

ps -e | grep name-of-process

In the screenshot below, we check if SSH is running on the system.

This also gives us the PID of the SSH daemon, which is 1963.

If you want to see all the processes that are running on your system, use the More pipeline.

You can also use the “top” command to see a list of running processes. This is useful because it shows how many system resources are being used by each process.

How does Linux kill processes? The PID, user, and name of the resource are all identified here, which is useful if you decide to terminate any of these services later.

Pay attention to the %CPU and %MEM columns, because if you find that an unimportant process is consuming valuable system resources, then killing it may help!

Another very effective way to get the corresponding process ID is to use the “pgrep” command. The only parameter you need to provide is the name (or part of the name) of the running process.

Here’s what it looks like when we search for SSH. As you can see, it returns process ID 2506.

Linux kills a process: Use a PID to kill a process

Now that we know the PID of the SSH daemon, we can use the kill command to terminate the process, as shown in the Linux kill process sample code below.

$ sudo kill 1963

You can issue the final “ps” command to make sure that the process is indeed terminated.

$ ps -e | grep ssh

The result is displayed as empty, which means that the process was successfully shut down. If you notice that the process is still running – which usually shouldn’t happen – you can try sending a different termination signal to the process, as described in the next session.

Note: It is not always necessary to use the “sudo” or root user account to end the process. In the previous example, we terminated the SSH daemon running under the root user. Therefore, we must have the appropriate permissions to end the process.

The default signal sent by the kill command

By default, the kill command will send a SIGTERM signal to the process you specify.

This should allow the process to terminate gracefully, as SIGTERM will tell the process to execute its normal shutdown procedure – in other words, it won’t force the process to end abruptly.

And that’s a good thing because we want our processes to close the way they’re supposed to.

However, there are times when the SIGTERM signal is not sufficient to terminate the process. If you run the kill command and notice that the process is still running, the process may still be going through its shutdown process, or it may have hung up completely.

Forced closure

To force a process shutdown and abandon a graceful shutdown, you can use the -9 switch to send a SIGKILL signal, as shown in the following Linux kill process usage example:

$ kill -9 processID

How does Linux kill processes? It can be tempting to always have a -9 switch attached to the kill command because it always works. However, this is not a recommended best practice. You should only use it on processes that suspend and refuse to shut down gracefully.

If possible, use the default SIGTERM signal. This will prevent errors in the long run, as it gives the process a chance to close its log files, terminate any lingering connections, etc.

In addition to the SIGTERM and SIGKILL signals, there are many other signals that can be sent to the process by kill, all of which can be seen via the -l switch.

The number next to the name is the number you specified in the “kill” command. For example, kill -9 is SIGKILL, like you can see in the screenshot above.

FOR DAY-TO-DAY OPERATIONS, SIGTERM AND SIGKILL MAY BE THE ONLY SIGNALS YOU NEVER NEED TO USE. Keep the others in mind in case you run into a strange situation where the process suggests terminating it with a different signal.

How do I kill all processes by name?

You can also use the name of the running process in the pkill command instead of the PID. Note, however, that this will terminate all processes running under the specified name, because kill doesn’t know which particular process you’re terminating.

$ pkill name-of-process

Linux Kill Processes: Consider the example below, where we terminated five processes with a single pkill command.

In this case, we only want to terminate one of the screen sessions; It is necessary to specify the PID and use the normal “kill” command. Otherwise, we can’t uniquely specify the process we want to end.

How do I kill all processes for a user?

You can also use the pkill command to terminate all processes run by Linux users. First, to see which processes are running under a specific user, use the ps command with the -u switch.

$ ps -u username

This screenshot shows us that there are currently five services running under the user “geek”. If you need to terminate all of them quickly, you can do it with pkill.

$ pkill -u username

Linux Kill Process Usage Example: How to Kill a Nohup Process?

You can terminate the nohup process like any other running process. Note that you can’t search for “nohup” in grep in the ps command, so you’ll need to search for running processes using the same method as above.

In this example, we find a script called “test.sh” that has been executed using the nohup command. As you’ll see, finding and ending it is very similar to the example above.

The only difference from the output is that the shell informs us that the process has been terminated. This is not part of the kill, but rather the result of running the script (in this case, the & symbol in the background) and connecting to the same tty that started the script.

$ nohup ./test.sh &

How do I run a process in the background?

Linux Ways to Kill Processes: The kill command is an effective way to terminate processes running in the background. You’ve already learned how to terminate a process in this tutorial, but knowing how to run a process in the background is a valid combination with the kill command.

You can append an ampand (&) to the command so that it executes in the background. This is useful for commands or scripts that take a while to execute, and you want to perform other tasks in the meantime.

Here we put a simple “ls” command in the background. Since it’s a command type that takes little time to execute, we’ll get more output about it getting the job done directly afterward.

The output in our screenshot says “Done”, which means that the job in the background has completed successfully. If you want to terminate the job, it will show “Terminated” in the output.

You can also move a job to the background by pressing Ctrl+Z on your keyboard. The ^Z in this screenshot indicates that we pressed Ctrl+Z and the test.sh script moved to the background.

By issuing the ps command, you can see that test.sh continues to run in the background, as shown in the Linux kill process sample code below.

$ ps -e | grep test.sh

Use on-screen commands

Another way to run a process in the background is to use the “screen” command. This is achieved by creating what is essentially equivalent to a separate terminal window (or screen along with the name).

How does Linux kill processes? Each screen you create has its process ID, which means it’s an efficient way to create background processes that can be terminated later with the kill command.

By default, not all Linux installations come with the Screen command, so you may have to install it.

On Ubuntu and Debian-based distributions, you can install it with the following command:

$ sudo apt-get install screen

Once the screen command is installed, simply type “screen” to create a new session:

$ screen

However, until then, it’s a good idea to get into the habit of giving names to the screen. This way, they can be easily found and identified later. All you need to specify a name is the -S switch.

$ screen -S my-screen-name

Let’s make a screen called “testing” and try to use the “kill” command to terminate it. Here’s how we started:

As soon as we enter this command and press enter, we are immediately taken to the newly created screen. This is where you can start the process you want to run in the background.

This is especially handy if you’re SSH connected to the server and need a process to continue running after the disconnection.

While your command/script is running, you can disconnect from the screen by pressing Ctrl+A and D (release Ctrl and A before pressing the D key).

As you can see, as soon as we detach the screen, the screen command lists the process IDs. Of course, we can use the kill command to terminate this screen (and the commands/scripts running in it).

You can easily find the process ID of a screen session using the following command:

$ screen -ls

If we don’t name our screen session with the -S switch, only the process ID itself will be listed. To reattach to any of the listed screens, you can use the -r switch:

$ screen -r name-of-screen

or

$ screen -r PID

In the screenshot below, we’re terminating the screen session we created (and whatever is running in it) and then issuing another screen -ls command to verify that the process has indeed ended.

Linux Kill Process Usage Example: How Do I Kill a Background Process?

In one of the examples in the previous section, we put the tesh.sh script running in the background. Most background processes, especially simple commands, will be terminated effortlessly.

However, just like any process, a background process may refuse to close easily. Our test.sh script has a PID of 2382, so we’ll emit the following Linux kill process sample code:

$ kill 2383

However, in the screenshot, you’ll notice that the script ignores our kill command:

As we’ve already learned, kill -9 is the best way to kill a process that is pending or refusing to terminate.

Linux Ways to Kill Processes: How to Kill Stopped Processes?

If they’ve accumulated and are no longer useful to you, it can be useful to kill all stopped background jobs at once. For example, we have three instances of a test.sh script running in the background, and they have been stopped:

$ ./test.sh &

You can use the ps command to view these processes:

$ ps -e | grep test.sh

Alternatively, to get a list of all stopped jobs on your system, you can run the jobs command:

$ jobs

How does Linux kill processes? The easiest way to kill all stopped jobs is to use the following command:

$ kill `jobs -ps`

Or use the -9 switch to ensure that the job is terminated immediately:

$ kill -9 `jobs -ps`

The ‘jobs -ps’ command will list the PIDs of all jobs running in the background, which is why we were able to combine its output with the kill command to end all stopped processes.

Example of Linux process killing: Termination is not allowed

If you get an “operation not allowed” error message when you try to terminate a process, it’s because you don’t have the proper permissions. Log in to the root account or use “sudo” (on Debian distributions) before executing the kill command, as shown in the Linux kill process sample code below.

$ sudo kill PID

I hope you found this tutorial useful.