Site icon DesignLinux

Kill Command in Linux

Kill Command in Linux

Kill command is a built in command in Linux which is used to terminate the process manually. This is commonly happening like, some applications may become unresponsive or start consuming a lot of system resources. In such situation the only solution is to either restart the system or kill the application process. You can terminate the process using several utilities but the kill command is most commonly used.

Make sure you are logged in as root or user with sudo privileges.

kill Command

The command behavior is slightly different between the shells and the standalone /bin/kill executable. Use the type command to display all locations on your system containing kill:

type -a kill
kill is a shell builtin
kill is /bin/kill

In the above output you can see that the shell built-in has priority over the standalone executable, and it is used whenever you type kill. You can use the binary by typing the full path to the file /bin/kill. Here, we are using the Bash built-in.

Following is the basic syntax for the kill command:

kill [OPTIONS] [PID]...

The kill command sends a signal to specified processes or process groups, causing them to act according to the signal. When the signal is not specified, it defaults to -15 (-TERM).

The most commonly used signals are:

You can get the list of all available signals, by using the -l option:

kill -l

Signals can be specified in three different ways:

Following commands are the same to each other:

kill -1 PID_NUMBER
kill -SIGHUP PID_NUMBER
kill -HUP PID_NUMBER

Terminating Processes Using the kill Command

To kill a process or terminate using kill command, first you need to find the process ID (PID). You can find PID using the different commands like top, ps, pidof and pgrep.

For example, to terminate the process of a chrome browser, you need to find the PID using pidof command:

pidof chrome

It will print the all process IDs of Chrome.

425 463 490 502

Once you have the process ID, you can kill them by sending the TERM signal:

kill -9 2551 2514 1963 1856 1771

You can combine the above commands into one, Instead of searching for PIDs and then terminating the processes:

kill -9 $(pidof chrome)

Reloading Processes Using the kill Command

The kill command is also used to send the HUP signal, which tells the processes to reload its settings.

For example, if you want to reload the Nginx server, you need to send a signal to the master process. The nginx.pid file located at /var/run which contains the process ID of the Nginx master process. Use the cat command to find the master PID:

cat /var/run/nginx.pid
10235

Once you found the master PID reload the Nginx settings by typing:

sudo kill -1 10235

Conclusion

The kill command is used to send a signal to processes and mostly SIGKILL or -9 signal is used to terminate the given processes.

If you have any questions or feedback, feel free to leave a comment.

Exit mobile version