• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Pidof Command in Linux

Written by Admin, Updated On October 3, 2020
pidof, terminal
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.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Bash Exit Command and Exit Code
Next Article   Modprobe Command in Linux

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook