Linux Kill command : Prevent runaway processes from hogging resources
Linux is a free, open source operating system, much used by the technical community. Being a command line system, it allows users a fine level of control that UI based operating systems cannot afford.
If you’ve used Linux for some time, you’ll know that programs get stuck now and again. Linux will automatically deal with these stuck programs, but sometimes you may have to do it manually. In this tutorial, we show you how to use the kill command in Linux. You can also use the kill command to stop unnecessary processes that are using up your PC resources. We assume you have a basic understanding of Linux.
Before you Begin to Kill a Process
Before you begin killing processes, make sure you’re not killing essential system processes. This may cause your system to crash. Killing the wrong process make your system invisible too. You will only be allowed to kill system processes if you’re a Root user. Also, only Root users can kill processes started by other users. All users can kill processes they’ve started themselves.
The kill Command
You can kill a process in Linux with the kill command. But you need to know its PID first. What is the PID of a process? Linux will give a process identification number to every process you’re running on the PC. For example, every time you play Solitaire, the program is assigned a PID. You can find out the PID of any process as long as you know the name of the process. Just type in the following command line:
For example, if you wanted to know the process ID of your Solitaire game, just type:
Now it’s time to kill the process. There are multiple ways to kill a process, as long as you know its PID:
kill -15 PID
kill -9 PID
kill –SIGKILL PID
Let’s say that the Solitaire program had a PID of 0809. To kill it, simply type:
kill -9 0809
What do all these negative numbers we tack on after kill mean? They are the kill signal numbers, which range from 1 to 64 (the numbers may vary according to the system version you’re running). Each kill signal has it’s a specific purpose.
For example, the number 1 kill signal (SIGHUP) will kill a child process that is a part of the parent process. It can also be used to start new processes. The most important kill signals, and probably the only kill signals you’ll ever need, are the signals 1, 3, 9 and 15. The kill signal 3 (SIGQUIT) interrupts a process and asks it to stop. However, the process can ignore this directive. It can also ask the process to produce a memory dump. Kill signal 9 won’t interrupt a process- it immediately stops it, without allowing it to clean-up. This command cannot be ignored. And finally we have kill signal 15 (SIGTERM). SIGTERM will allow a process to shut down and clean-up. It also allows the program to release memory.
By default, Linux uses the number 15 signal (SIGTERM) for killing a process. This means that you don’t need to specify a signal number or signal name if you know the process ID. If you wanted to kill your Solitaire program, just type the following command:
You can use either numbers or the names of the signals to kill a process. If you are going to type in the name of the kill signals, make sure they are all upper case. Otherwise Linux will not register them.
Making Sure the Process has Stopped
Now, just verify if your process is gone by using the pidof command that was given earlier. Alternatively, you can also use the ps command, which has the following syntax:
ps aux | grep solitaire
If your process hasn’t stopped, it may mean that the kill signal you used wasn’t strong enough or you didn’t have the privileges to stop it. You can use more powerful signals in this case, or you can use the sudo command if you’re logged in as a root user, or a combination of both. The sudo command is a administrative level command which can be used in combination with the kill command:
sudo kill -9 0809
sudo kill –SIGKILL 0809
Signal 9 cannot be ignored by the system, so it should be enough to close even the most obstinate process.
The killall command
What do you do if you want to kill multiple processes at once? You can use the killall command for this. For example, if you have Mozilla Firefox installed on your PC and you want to close all instances of the program, along with its update processes, you can use the kill all command. In this instance, you don’t need to know the process ID, but just the name of the program. You can use the following command for it:
killall –SIGQUIT firefox
If you don’t know the PID of the program or if you want to kill one instance of a program by just its name, you can use the following command:
killall -9 firefox
The pkill command
The pkill command is a very useful command. If you had several programs running from the same publisher (like Mozilla), you can kill all of them with the pkill command. The pkill command (partial kill) command will kill any processes with names beginning with the letters you specify. If you wanted to kill Mozilla processes, for example, you would type in the following code:
pkill -9 mozi
This will kill all processes that begin with “mozi”.
The xkill command
The xkill command is another very useful command. It can be used if your browser window has frozen, for example, and you need to close it quickly. You can use the xkill command by simply typing in xkill in command line tools. Your cursor should change if you typed it in correctly. Now just click on any window that you need closed. The application will shut down and all its processes will be stopped.
To recap it all, you can use one of 4 commands to kill processes in Linux: xkill, pkill, killall and kill. If you’re not going to be a system administration, you should be fine with knowing just the xkill and pkill commands. Otherwise, we recommend you master controlling the system with the Linux command line.
Top courses in Linux
Linux students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.