Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Pidof Command in Linux - DesignLinux

Oct 03 2020
designlinux 0 Comments
Pidof Command in Linux

The pidof is a command-line tool used to find the process ID of a running program. This article shows you how to use the pidof command in Linux.

How to Use the pidof Command#

pidof is implemented differently on Red Hat and Debian based distributions. On Debian, sysvinit-utils package includes it while on Red Hat it is a part of the procps-ng package. We will discuss the common options.

Following is the basic syntax for the pidof command:

pidof [OPTIONS] PROGRAM_NAME

It can take one or more names as an arguments but generally, it is used for only one name to get pidof.

If you invoke without any arguments it will show the PIDs of all running programs that match with the given name.

For example, to find the PID of the SSH server, you would run:

pidof sshd

Here, it shows the PIDs of all running process which matchs with sshd. If no match will found then it will be empty.

7022 6911 921

pidof returns exit code 0 when at least running program match with given name else it will return 1 as exit code. It’s used while you writing the shell scripts.

By default, all PIDs of the matching running programs are displayed. Use the -s option to force pidof to display only one PID:

pidof -s program_name

Use the -o option to exclude a process with a given PID from the command output:

pidof -o pid program_name

When use -o option with pidof command, you can use a special PID named %PPID that represents the calling shell or shell script.

You can use the -c option to return only the PIDs of the processes that are running with the same root directory.

Make sure this option can be used if you are running as root or user with sudo privileges:

sudo pidof -c pid program_name

Example Usage of the pidof Command#

You can use the pidof command in the combination with the kill command to terminate a program.

For example, if the Google Chrome browser is unresponsive, and you want to kill the Chrome processes. Find the PIDs, with pidof:

pidof google-chrome

It will show all Chrome processes:

2520 3564 8564

Once you have the Chrome processes PIDs, send the SEGTERM signal to terminate all of them:

sudo kill -9 2520 3564 8564

You can terminate in one command using the command substitution expression $(…)

sudo kill -9 $(pidof google-chrome)

Conclusion#

You can use the pidof command to find out the PIDs of a specific running program. Generally it is used with the name of the program for which you are searching.

If you have any questions or feedback, please leave a comment below.

Related

Tags: pidof, terminal

How to Switch (su) to Another User Account without Password

Prev Post

Modprobe Command in Linux

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
  • 1
  • 595
  • 609,954

DesignLinux.com © All rights reserved

Go to mobile version