(: August 7, 2019)
Moments come when a process in your Linux system begins to behave weirdly and you as an Administrator needs to stop it for the good of the health of the whole system. The following utilities will help you find the process and stop it.
Before we proceed, as we know, every Linux process has a process identification (PID) and we will have to look for the particular PID of interest. For this, we can use the following tools
Using ps command
This command reports a snapshot of the current processes.
To list all processes, just run
$ ps -A
To filter your output, you can use pipes to get to the exact process you need, for example
$ ps aux | grep apa
This will list all processes that have the above letters in their process names. From there, you can narrow down and get the exact PID of your culprit.
As a substitute in your armory of commands, you can also use pidof or pgrep as follows
$ pidof zsh
That will return the PID of zsh process
$ pgrep zsh
That will also return the PID of the zsh process.
After you discover the PID, the next step is to stop the process. There are various tools here for example: kill, killall, pkill, top, and others.
You have to realize that these commands work as long as you have the right permissions to stop the processes themselves. If you do not have sufficient rights to kill other users’ processes, they won’t work. You will be able to kill your own processes though.
The root user as it can be guessed is the superuser and can kill any process belonging to any other user in the system.
Kill process on Linux using kill command
kill sends a signal to a process. The signals can either be:
- SIGHUP which can be represented with 1 and causes the process to Hangup
- SIGKILL which can be represented with 9 and causes the process to be Killed ungracefully
- SIGTERM which can be represented with 15 and causes the process to be Killed gracefully
If the kill command is executed without options, SIGTERM is used by default.
Examples of kill in action
Kills process with PID 7089 without saving any open files
$ kill -9 7089 $ kill -SIGKILL 7089
Kills process with PID 7080 after it has made sure all of its files are saved. This is gracefully killing of the process:
$ kill -15 7080 $ kill -SIGTERM 7080
Another command that you can take advantage of is the killall command. If you know the name of the process, there is no need of finding its PID. killall kills it by name as shown below.
For more options, run:
$ man kill
Kill process on Linux using killall command
The killall command kills processes by their name as prescribed above. Just do:
$ killall httpd
To learn more, check man page:
$ man killall
Kill process on Linux using pkill command
Signals processes based on name and other attributes
pkill works in a similar way as killall. Its syntax is as simple as:
$ pkill [options] pattern
pkill sends signals to processes based on name and other attributes.
Using pkill is very simple if you know the name of the process to pass the command and the name of the process as shown below. It uses SIGTERM signal by default but it can be changed if you like.
$ pkill nginx
All command options:
$ pkill --help Usage: pkill [options] <pattern> Options: -<sig>, --signal <sig> signal to send (either number or name) -e, --echo display what is killed -c, --count count of matching processes -f, --full use full process name to match -g, --pgroup <PGID,...> match listed process group IDs -G, --group <GID,...> match real group IDs -i, --ignore-case match case insensitively -n, --newest select most recently started -o, --oldest select least recently started -P, --parent <PPID,...> match only child processes of the given parent -s, --session <SID,...> match session IDs -t, --terminal <tty,...> match by controlling terminal -u, --euid <ID,...> match by effective IDs -U, --uid <ID,...> match by real IDs -x, --exact match exactly with the command name -F, --pidfile <file> read PIDs from file -L, --logpidfile fail if PID file is not locked --ns <PID> match the processes that belong to the same namespace as <pid> --nslist <ns,...> list which namespaces will be considered for the --ns option. Available namespaces: ipc, mnt, net, pid, user, uts -h, --help display this help and exit -V, --version output version information and exit
Kill process on Linux using top command
You can use the top command to kill a process through its interactive session while it is running.
To kill processes directly from the “top” interface, press “k“. It will ask you for the PID of the process to kill. Enter the PID and press enter. It uses
top - 10:07:55 up 1 day, 22:09, 0 users, load average: 0.52, 0.58, 0.59 Tasks: 4 total, 1 running, 3 sleeping, 0 stopped, 0 zombie %Cpu(s): 4.7 us, 3.5 sy, 0.0 ni, 91.0 id, 0.0 wa, 0.7 hi, 0.0 si, 0.0 st KiB Mem : 16669896 total, 2348244 free, 14092300 used, 229352 buff/cache KiB Swap: 50331648 total, 49101952 free, 1229696 used. 2443864 avail Mem PID to signal/kill [default pid = 1] 10000
It uses SIGTERM signal by default as shown below. Just press enter again and top will do the rest.
top - 10:07:55 up 1 day, 22:09, 0 users, load average: 0.52, 0.58, 0.59 Tasks: 4 total, 1 running, 3 sleeping, 0 stopped, 0 zombie %Cpu(s): 4.7 us, 3.5 sy, 0.0 ni, 91.0 id, 0.0 wa, 0.7 hi, 0.0 si, 0.0 st KiB Mem : 16669896 total, 2348244 free, 14092300 used, 229352 buff/cache KiB Swap: 50331648 total, 49101952 free, 1229696 used. 2443864 avail Mem Send pid 10000 signal [15/sigterm]
To learn more, check man page:
$ man top
You now know how to kill that bogus or annoying Linux process using simple Linux tools that usually come pre-installed in almost all distributions. Thank you for reading through. If you are interested, you can peruse the guides below with similar topics.