
The kill command is commonly used to terminate running processes, it takes the process ID or name as its arguments and sends a signal to the process and the process will act according the the sent signal.
Table of contents.
- Introduction.
- Syntax.
- Commands.
- Summary.
- References.
Introduction.
The kill command is commonly used to terminate processes in Linux, give a processes id PID it sends the default signal SIGTERM which terminates the process.
Keep in mind that processes can only be killed by the owner(user), a user cannot kill processes owned by other users, a user cannot kill processes used by the system and finally a root user can kill any process either owned by the system or any other user.
Syntax.
The syntax is as follows,
kill [signal or option] PID(s)
Various options for kill command are,
- -l, to list all signal names.
- -s -signal, to specify the signal name.
- -n sig, to specify the signal number.
One can also use killall which kills multiple processes at the same time.
Various options for killall are,
- -e, --exact, to check for exact matches in case of very long names.
- -g, --process-group, to kill a process group.
- -y, --younger-than, to kill processes younger than specified time.
- -o, --older-than, to kill processes older than specified time.
- -i, --interactive, to ask for confirmation before killing a process.
- -l, --list, to list all signal names.
- -q, --quiet, to kill processes without printing any output.
- -s, --signal SIGNAL, to send a signal instead of a SIGTERM.
- -u, --user USER to kill processes by a specific user.
- -n , --ns PID, kill processes in the same name-space as the PID.
- -r, --regexp, kills processes matched by a regular expression.
- -v, --verbose, to print out debugging information.
- -I, --ignore-case, matches a processes while ignoring case sensitivity.
The pkill command kills a process based on it's name.
Commonly used options include,
- -n, to kill the newly discovered processes.
- -o, to kill the older processes.
- -u, to kill processes owned by a specified user.
- -x, to kill processes matching a specified pattern.
- -signal to kill processes with a specified signal.
Commands.
To view currently running processes we can either use top, ps command or htop commands.
An example
To list all running processes we write,
ps -a
or
ps -aux
Process IDs
A PID can be,
- > 0, (greater than 0) in such a case, the signal is sent to a process with an ID equal to the PID.
- == 0, the signal is sent to processes in the current process group, i.e processes in that GID of the shell that sent the signal. To get ids execute the id command.
- == -1, the signal is sent to all processes with the same UID as the user who executes the command.
- < -1, the signal is sent to all processes in the process group where GID is equal to the absolute value of the PID.
To get a process' id we can write,
pidof bash
The above command returns the PID os bash process.
Terminating processes
The kill command sends a signal to a specified process or group of processes. The signal specifies how a process will be affected. It can be killed, reloaded.
To list all signal names we write,
kill -l
Commonly used signals are 1 which reloads a process, 2 which sends an interrupt from the keyboard signal, 9 which sends a kill signal to kill the process, 15 which kills a process gracefully and 17, 18, 23 which sends a signal to stop a process.
An example
To send a SIGKILL signal to a process with a PID 2342, we write
kill -9 2342
We can also write,
kill -SIGKILL 2342
We can also kill multiple processes by passing multiple process ids as follows,
kill -9 7888 2313 6719 6671
We can also kill a process by its name as follows,
kill firefox
The command kills a firefox process currently running.
Assuming there are multiple instances of firefox running we can use the killall command,
killall -9 firefox
or by using pkill command,
pkill firefox
To reload a process's settings we can send the SIGHUP signal,
kill -1 2278
Where 2278 is the PID of the process we want to reload.
Summary.
We use ps, top or htop command to get PIDs which we then use to kill a process. We can also kill a process using the pkill command using its name.
The killall kills all instances of a process.
You can also kill processes while in the htop or top windows.
References.
- Linux processes: signals, termination, zombies, cleanup.
- Execute man kill or kill --help.
- For process signals execute kill -l.
- htop