Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Pkill Command in Linux - DesignLinux

Oct 08 2020
designlinux 0 Comments
Pkill Command in Linux

The pkill is a command-line utility, used to send signals to the processes of a running program. The processes can be specified by their names, by a user name, or other attributes. In this article we will discuss about pkill command in Linux.

By default the pkill command comes pre-installed as a part of the procps package on most of Linux distributions.

Use of pkill Command#

The basic syntax for the pkill command is as follows:

pkill [OPTIONS] [PATTERN]

The matching [PATTERN] is specified using extended regular expressions.

If you run the pkill command without any option, it sends the 15 (TERM) signal to the PIDs of all running programs that match with the given name. For example, to stop the all processes of the Google Chrome, you would run:

pkill -9 google-chrome

It will returns 0 as exit code if any one running process match otherwise it will returns 1. Exit codes are useful while you are writing shell scripts.

To send the different signals to the matched process use the --signal option followed by the signal number or name.

To get the list of all available signals use kill -l command:

Following are the most common signals:

  • 1 (HUP): to reload a process.
  • 9 (KILL): to kill a process.
  • 15 (TERM): to gracefully stop a process.

Signals can be specified in three different ways:

  • using a number (e.g., -1)
  • with the “SIG” prefix (e.g., -SIGHUP)
  • without the “SIG” prefix (e.g., -HUP).

For example, to reload the Nginx processes you would run:

pkill -HUP nginx

pkill uses regular expressions to match the processes names. It is always a good idea to use the pgrep command to print the matched processes before sending signals to them.

For example, to list all processes that contain “ssh” in their names:

2045 sshd
2154 ssh-agent
8506 ssh
1259 ssh-agent

If you want to send a signal only to the processes which names are exactly as the search pattern, you would use:

pkill '^ssh$'

pkill command matches the process name by default. You would use -f option to match against full argument lists.

pkill -9 -f "ping 8.8.8.8"

Use -u option with pkill to match processes being run by a specified user:

pkill -u kunj

Specify the multiple users by separating their names with commas:

pkill -u kunj,tecnstuff

You can combine options and search patterns. For example to send KILL signal all processes that run under user “kunj” and contains “gnome” in their names you would type:

pkill -9 -u kunj gnome

Use the -n option to get most recent and -o for oldest started process.

For example, to kill the most recently created screen:

pkill -9 -n screen

Conclusion#

This article shows you how to use the pkill command to send signals to running programs based on different criteria. To learn more about the pkill command, take a look at pkill man page.

Feel free to leave a comment if you have any question or feedback.

Related

Tags: pkill, terminal

How to Monitor Node.js Applications Using PM2 Web Dashboard

Prev Post

How to Install PostgreSQL and pgAdmin in RHEL 8

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 667
  • 610,026

DesignLinux.com © All rights reserved

Go to mobile version