Oct 14, 2023 3 min read

Pidof Command in Linux

Use pidof command in linux with our step-by-step tutorial. It is used to find the process ID (PID) of a running program.

Pidof Command in Linux
Table of Contents

Introduction

Before we start discussing about pidof command in linux, let's first understand-What is a Pidof Command ?

The pidof command in Linux is used to find the process ID (PID) of a running program. It allows you to search for the PID of a process using its name. The pidof command is particularly useful when you need to identify the PID of a program for further process management, monitoring, or troubleshooting purposes

In this tutorial, we'll show you how to use the Linux pidof.

How to Use the pidof Command

For Red Hat and Debian-based distributions, there are many pidof implementations. The pidof command is part of the procps-ng package on Red Hat distributions, while it is part of sysvinit-utils on Debian. We'll go over the choices that both implementations have in common.

The pidof command has the following syntax:

pidof [OPTIONS] PROGRAM_NAME

Although pidof allows zero or more names as parameters, you should usually only give it one.

PIDs of all running programs that match the supplied name will be printed if pidof is called without any options. To find the PID of the SSH server, for example, type:

pidof sshd

The PIDs of any running processes with names that match sshd will be displayed on the screen. The output will be empty if no matches are detected.

Output

4382 4368 811

When at least one running program matches the given name, pidof returns 0. Otherwise, 1 is the exit code. When building shell scripts, this can come in handy.

Use the entire pathname to the program as a parameter to ensure that just the PIDs of the application you're looking for is displayed. If you have two running programs with the same name in two distinct folders, pidof will display the PIDs of each of them.

All PIDs of the matching running applications are displayed by default. To compel pidof to display only one PID, use the -s option:

pidof -s program_name

You can use the -o option to exclude a process with a specific PID from the command output:

pidof -o pid program_name

When using the -o option with pidof, a special PID named %PPID can be used to represent the calling shell or shell script.

Use the -c option to get just the PIDs of processes that are executing in the same root directory.

This option is only applicable in certain circumstances. pidof is executed as root or as the sudo user:

pidof -c pid program_name

Example Usage of the pidof Command

The following example demonstrates how to terminate a program using the pidof command in conjunction with the killcommand.

Assume that your Firefox browser has become unresponsive, and you need to terminate the Firefox processes. To begin, use pidof to locate the PIDs:

pidof firefox

All Firefox processes will be printed with this command:

Output

2551 2514 1963 1856 1771

Send the SEGTERM signal to terminate all Firefox processes once you know their PIDs:

sudo kill -9 2551 2514 1963 1856 1771

To terminate the program in one command, use the command substitution expression $(...):

sudo kill -9 $(pidof firefox)

FAQs on Pidof Command in Linux

How do I use the pidof command to find the PID of a program?

To find the PID of a program using pidof, simply provide the name of the program as an argument to the command. For example, pidof firefox will provide the PID of the Firefox browser if it is currently running.

Can the pidof command find multiple PIDs for the same program?

Yes, the pidof command can return multiple PIDs if there are multiple instances of the same program running simultaneously.

What happens if the program specified in pidof is not running ?

If the program specified in the pidof command is not running, the command will return no output. It will not display any PIDs.

Can I use regular expressions with the pidof command?

No, the pidof command does not support regular expressions. It only searches for exact program names.

What is the difference between pidof and pgrep?

Both pidof and pgrep can be used to find PIDs of running processes. However, pgrep provides more advanced search functionality, allowing for regular expressions and more extensive process matching options.

Can pidof be used with wildcard characters to find PIDs?

No, the pidof command does not support wildcard characters. It searches for exact program names.

Is the output of pidof useful for further processing in scripts?

Yes, the output of the pidof command can be captured and used in scripts for further processing. It can be assigned to variables or passed as arguments to other commands.

Conclusion

The pidof command is used to determine the PIDs of running programs.

pidof is a straightforward command with few options. In most cases, you'll merely use pidof with the name of the program you're looking for.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.