• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

How To Use pkill In Linux And Why it’s Useful

Bhagwad Park

Published on: August 14, 2024

Categories: Linux Command Line, VPS Hosting 0

Pkill is a Linux command-line utility that allows you to send signals to currently running processes. The most common signal is a kill signal, from whence comes its name. But killing a process is just one of the things you can do. You can also suspend and resume them, send user-defined signals, and even notify them. In this article, I’ll show you how to use pkill, when to use it, and how it’s superior to the more straightforward “kill” command.

Table of Contents
  • How to Use pkill
  • Pkill Pattern Matching
    • Default Pattern Matching
    • Extended Regular Expressions
    • Case Sensitivity
    • Matching the Exact Command Used to Start the Process
    • Matching Processes Started by Users
  • Previewing the Targets of Pkill
  • When to Use Quotes for Patterns
  • Conclusion

How to Use pkill

We can use pkill to kill a process by specifying its name. Unlike with “kill”, we can’t use the process ID so you need to know its name.

So let’s say I use a sleep command and send it into the background using the “&” character like this:

sleep 300 &

The “&” character means that you can continue working on the terminal while the process executes in the background. You can use this symbol in conjunction with the nohup command to create a background process that won’t terminate even after you leave the command line.

After creating the command and sending it into the background, you can see that it runs with a certain process ID using the command:

ps

And here’s the output;

Sleep Command for 5 Minutes
Sleep Command for 5 Minutes

Here, the sleep command has process ID 25179. But since we can’t use the process ID for pkill, we can just use the command name instead. To kill the sleep command, we use:

pkill -9 sleep

The number “9” is the number assigned to the “SIGKILL” process. If you want, you can also use the following:

pkill -KILL sleep

Each signal is associated with both a number as well as a name. You can use the number if it’s faster, but I think the name is easier to remember.

This gives the following output:

Killing the sleep Command
Killing the Sleep Command

As you can see, after I execute the sleep command, it says “Done” and the next time I run the “ps” command, it’s no longer there in the process list.

So that’s how you use a basic pkill command. There’s nothing complicated about it.

Pkill Pattern Matching

One of the best reasons to use pkill over kill is the former’s ability to use pattern matching to target processes. While the kill command strictly requires process IDs, it makes it hard to use consistently since process IDs aren’t the same every time you start the same process. By contrast, by focusing on the process name, administrators can share commands and scripts between themselves and ensure that the right processes will receive the signals they want.

Default Pattern Matching

By default, pkill’s “name” argument is a pattern that matches the entirety or just a substring. For example, the command:

pkill apache

Will affect all processes with the name “apache”. That means it will match both “apache” and “apache2”. This can be useful if you don’t know the version of apache that’s running and you want to shut it down regardless.

Naturally, this can also be dangerous. You want to make sure that the pattern you’re using is as tight as possible and can’t affect any other processes. Pkill has the potential to send signals to multiple processes at once, and if your pattern is too broad, it could have disastrous consequences like killing a whole bunch of processes that you didn’t intend to kill! You need to make sure, for example, that the pattern you’re using isn’t accidentally a substring of another, unrelated process.

Extended Regular Expressions

Pkill allows you to use regex to more accurately target processes. For example, consider the following command:

pkill '^httpd'

“httpd” is a process normally associated with the Apache web server. The “^” character indicates that the process name starts with httpd. This precaution ensures that processes containing “httpd” in the middle won’t be collateral damage.

Similarly, we can use the “$” character to indicate the end of a string as shown here:

pkill 'nginx$'

The above command will apply to all processes ending with “nginx”.

Case Sensitivity

By default, pkill pattern matches are case-sensitive. If, on the other hand, you want to tell pkill to ignore case, you can use the “-i” parameter like this:

pkill -i sshd

The above will match “SSHD” and “sshD” as well.

Matching the Exact Command Used to Start the Process

If the above techniques seem a bit broad to you, it’s understandable. After all, when considering which processes to kill, you might want to be as specific as possible. In the absence of process IDs, pkill still provides you with a highly specific way to target processes – namely the command that was used to start them.

To do this, we use the “-f” parameter like this:

pkill -f 'python my_script.py'

The above command will match the process that started with exactly the command “python my_script.py”. This kind of targeting will ensure that you don’t accidentally kill the wrong process.

Matching Processes Started by Users

Because Linux is a multi-user operating system, more than one user may run a command with the same command line. As regular users, pkill doesn’t allow us to kill processes started by other users, so you don’t need to worry about affecting what someone else has done. However, if you’re running as root, then you can see all the processes that everyone has started and kill them as well.

As root, to avoid killing the wrong person’s process, you should use the “-u” option to specify the user. For example:

pkill -u user1 sshd

The above command will target the process “sshd”, but only if it belongs to the “user1” user. Since SSHD is such a common process, such commands are necessary to ensure that you target the right users.

Previewing the Targets of Pkill

Since it’s so easy to kill processes with pkill, it’s a good idea to verify in advance that you’re targeting the right process. Doing so is easy with the “pgrep” command. For example, the following command:

pgrep -fl bash

Will show you the list of running processes that match your criteria. It won’t take any action on them, or kill them, but at least you can see what’s being targeted. The “-l” flag tells pgrep to show you the names of the processes along with the process IDs. Running this on my command line, I get this:

Preview Targets
Preview Targets

As you can see, I have two processes running, and the command shows me that it will target “bash” along with the process number. Without “-l”, I would just get the process number.

When to Use Quotes for Patterns

You can use pkill without quotes for the patterns for casual use cases. However, there are two scenarios where you must use quotes like this:

pkill -f 'python my_script.py'

First, if you have spaces in the pattern, you must use quotes. Second, if you’re using regular expressions, then quotes are necessary. It might be a good idea to get used to using quotes in general for pkill, given the stakes. Better safe than sorry!

Conclusion

Pkill is a great tool to easily kill stuff when you don’t want to bother with the process ID. This is useful when you want to create reusable code that works all the time. There are many ways to more carefully target which processes are affected, and you should use the “pgrep” tool to ensure that you’re not accidentally killing something that you shouldn’t.

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Related Posts

Using Bash Job Controls to Manage Multiple Processes

Bash job controls are used to send long running tasks into the background, and bring them forward when necessary.

How to Use Vim Search and Replace in Linux

The vim search and replace functionality in Linux consists of a series of very powerful tools - but also simple ones to get started easily.

Curl vs Wget: Choosing the Right Tool for the Job

Linux has two popular tools for getting data from third party websites. Here's the difference between curl and wget.

When to Use Here-String vs Heredoc in Bash

Heredoc and Here-string are both ways to input strings into commands that read from stdin. Here's the difference between them.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA