Linux Command - kill

kill - terminate a process by sending a signal.

The command kill sends the specified signal to the specified process or process group. If no signal is specified, the TERM(15) signal is sent.

kill command kills a process by its PID. To kill a process by name, use killall command.

Syntax

1
2
3
kill [-s signal] pid...
kill -signalnumber pid...
kill -l

Options

Option Description
-s signal specify the signal to send, default is TERM
-p pid… list of processes kill should signal
-l to list signal names

pkill command

If you know the name of the process, you can use pkill [OPTIONS] <PATTERN> to kill all running programs that match with the given name. To kill all python3 process:

1
pkill node # kill all node applications

Example

kill a process

kill without option will send SIGTERM signal. Use ps aux | grep <search keywork> to get the process id. Then use kill command to kill the process.

1
kill 16575

Send KILL signal to a process

1
kill -9 16575

or

1
kill -s KILL 16575

List signal names

1
kill -l

To kill a process by pid

use ps aux or pgreg command to get the process’s PID.

1
ps aux | grep node

In MacOS, if you only know the port number used by the application, you can get the PID and command using the following command

1
sudo lsof -iTCP -sTCP:LISTEN -n -P

With the process id, You can use kill <pid> to terminate the program. The default sends SIGTERM signal. You can use kill -9 <pid> command to send SIGKILL to terminate immediately. SIGKILL is hard kill.

1
kill -9 9058

Reference