This guide will walk you through the basics of managing processes in Linux. By the end of this tutorial, you will have a basic understanding of how to manage your processes straight from the terminal
Managing processes in Linux can be a bit tricky if you are just getting started. In the Linux world, everything is a process, and managing these processes can be daunting. With so many options and commands available, it can be challenging to know where to start.
First, let's start with the basics:
What is a process in Linux?
A process is simply an instance of a running program. So every time you open up a new terminal and run a command or a program, that's a new process. Processes can be either running or sleeping. When a process is sleeping, it is not using any CPU resources.
Use the ps
command to see all the processes running on your system. It will show you a list of all the processes, their PIDs (process IDs), and some other information about them.
ps
Returns:
j4y@DESKTOP-71FSHR2:~$ ps
PID TTY TIME CMD
10 tty1 00:00:00 bash
72 tty1 00:00:00 ps
The ps
command is extensive. You can do a lot with it, but that is a story for another time. For now, let's focus on how to kill a process.
As mentioned earlier, processes can be either running or sleeping. Depending on which state they're in, they are killed differently.
To stop a running process, you can use the kill
command.
To kill a sleeping process (one that has been started but is not currently running), you can use the killall
command.
Using the kill command
To kill a process, you have to know its PID first. By running the command ps
, you get a list of all processes with their PID. Once you have the PID, use the kill command to send a signal to the process telling it to stop running. For example, if I wanted to kill the process with PID 1234, I would use the following command:
kill 1234
It sends a SIGTERM
signal to the process with PID 1234, telling it to terminate.
There are other signals that you can send to processes as well. For example, SIGKILL
is a signal that will kill the process immediately, without giving it a chance to clean up first. Therefore, this command should only be used as a last resort when other signals have failed.
All kill Signals
The kill command may be used with a number of different signals, To check the other signals available, use the kill command with the -l
option:
kill -l
Returns:
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
The most commonly used signals are SIGTERM (15)
and SIGKILL (9)
.
We often find ourselves using
ctrl + c
orctrl + z
, which are also kill signals.
SIGSTOP (19)
is triggered every time we pressctrl + z
.SIGCONT (18)
- (Sig Continue) triggers when we pressctrl + z
the second time.SIGINT (2)
- (Sig interrupt) triggers when we pressctrl + c
You can specify these signals by name or number. For example, to kill a process with a PID of 1234, you would run:
kill 1234
or
kill -15 1234
or
kill -SIGTERM 1234
These three commands do the same thing. They send the default "terminate" signal to the process (15), which is usually what you want. If the process refuses to die after that, you can try sending it a SIGKILL
signal:
kill -SIGKILL 1234
or
kill -9 1234
This command will kill the process immediately.
Using the killall command
The killall
command kills all processes that are running a specified program. It is useful when a program is frozen or unresponsive, and you want to force it to quit. For example, if you want to kill all processes that are running the Firefox web browser, you would use the following command:
killall firefox
This command will send a SIGTERM
signal to all processes associated with firefox.
The killall
command can also be used to kill multiple programs at once. To do this, list the names of the programs you want to quit, separated by spaces. For example:
killall firefox chrome
Using pkill command
If you want to get a little more granular with your killing, you can use the pkill
command which allows you to kill processes based on their name rather than their PID.
So, for example, if I wanted to kill all processes named "firefox", I would run:
pkill firefox
The pkill
command can also kill all instances of a program. For example, having ten instances of firefox running, Using pkill firefox would kill them all.
That is all there is to killing processes in Linux.
Wrapping Up
As you can see, there are various methods for killing processes in Linux. Different signals can also be used depending on what you're trying to accomplish. In most cases, the kill
and killall
commands will be all you need. However, if you're having trouble with an unresponsive or stuck process, the kill -9
signal may be necessary.
Questions? Feel free to leave a comment below. Alternatively, check out the man pages for the ps and kill commands (man ps and man kill). Finally, if you found the article helpful, consider sharing it.
Thanks for reading!