Finding and Killing Processes
The pkill command can be used to stop / kill all processes that match its pattern from the command line:
pkill -aif warp
If you want to be more precise, the kill command can be used to quit a particular process by specifying its process ID:
kill 2934
There are a number of ways you can discover the IDs of running processes depending on what you know about the process.
Searching For The Right Process To Kill
You can search for a process by:
- The command name that was executed to spawn it:
pgrep -aifl firefox - The user that executed it:
pgrep -u root - A network port it's using:
lsof -i tcp:8888 - A file it has open:
lsof /dev/null
Force Killing Processes
If the process doesn't end, you can force kill the process by sending it the SIGKILL signal. To do this, simply add the -9 argument to kill or pkill:
pkill -9 -aif firefox
pkill and pgrep vs. killall and ps
killall and ps are the old-fashioned equivalents of pkill and pgrep. They're now generally discouraged because they behave very differently and are somewhat hard to use. Some versions of Linux may not even provide killall.
If you still want to use killall, you might have difficulty targeting the right process, but you can try the following:
killall Firefox
💡Tip for Warp Users
If these commands don't find any matching processes, they will often silently exit with no notice that it didn't work other than a nonzero return code. Warp users will notice that the terminal block is helpfully made red to indicate that the process failed.
Process Signals
By default, running kill or pkill sends the SIGTERM signal to the specified process, which is the polite way to ask the process to terminate. The process can choose to ignore or handle this signal rather than ending, either purposefully or because the process is misbehaving.
Signals you can send include:
- 2 - SIGINT - Interrupt the program (equivalent to pressing Ctrl+C on the terminal)
- 9 - SIGKILL - Forcibly terminate the program immediately (cannot be ignored or handled)
- 15 - SIGTERM (default) - Request that the program terminate (can be ignored or handled)
Command Portability
Some of these commands and their flags may behave slightly differently on different operating systems. Favoring pkill (instead of killall) and pgrep (instead of ps) can help reduce these issues. All of the specific invocations listed above will work on both Linux and MacOS.
Written by
Shawn Tabai
Software Engineer, Actual
Filed Under
Related Articles
List Open Ports in Linux
Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.
Count Files in Linux
Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.
How to Check the Size of Folders in Linux
Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.
Linux Chmod Command
Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.
POST JSON Data With Curl
How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.
Format Command Output In Linux
Learn how to filter and format the content of files and the output of commands in Linux using the awk command.
Create Groups In Linux
Learn how to manually and automatically create and list groups in Linux.
Switch Users In Linux
Learn how to switch between users, log in as another user, and execute commands as another user in Linux.
Remover Users in Linux
Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.
Delete Files In Linux
Learn how to selectively delete files in Linux based on patterns and properties using the rm command.
Find Files In Linux
Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.
Copy Files In Linux
Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.