Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the Linux pidof command

Shulou Source: shulou.com Published: 2022-06-01 21:11:43 09月14日 Update

In this article, the editor introduces in detail "how to use the Linux pidof command", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use the Linux pidof command" can help you solve your doubts.

Pidof is a tool used in Linux systems to find the process number (pid) of a running process, which is similar to pgrep and ps. The pidof command is used to find the process number id number of the process with the specified name.

How to use the pidof command

For distributions based on Red Hat and Debian, there are different implementations of pidof. In the Red Hat distribution, the pidof command is part of the procps-ng package, while on Debian, it is part of sysvinit-utils. We will introduce the options common to both implementations.

The syntax of the pidof command is:

Pidof [option] Program name

This command takes zero or more names as parameters, but typically you pass only one name to pidof.

When called without any options, pidof prints the PID of all running programs that match the given name. For example, to find the PID of vim, run:

[linuxidc@linux:~/www.linuxidc.com] $pidof vim4942

If there is a running process with a name that matches sshd, its PID is displayed on the screen. If no match is found, the output will be empty.

Output

4942

Pidof returns 0 when at least one running program matches the name of the request. Otherwise, the exit code is 1. This is useful when used in shell scripts.

To ensure that only the PID of the program you want to search is displayed, use the full pathname of the program as a parameter. For example, if you have two running programs with the same name in two different directories, pidof will display the PID of both running programs.

By default, all PID of the running program that match is displayed. Use the-s option to force pidof to display only one PID:

$pidof-s program name

The-o option allows you to exclude processes with a given PID from the command output:

$pidof-o pid program name

When pidof is called with the-o option, a special PID named% PPID can be used to indicate that the Shell program or Shell script is called.

To return only the PID of processes running with the same root directory, use the-c option.

This option applies only to pidof running as root or sudo:

$pidof-c pid program name

Examples of usage of the pidof command

The following example shows how to use a combination of the pidof command and the kill command to terminate a program.

Assume that the Firefox browser is unresponsive and you need to terminate the Firefox process. First, use pidof to find PID:

[linuxidc@linux:~/www.linuxidc.com] $pidof firefox

This command prints all Firefox processes:

Output

5231 5207 5166 5131 5090

Once it is known that Firefox has processed PID, a SEGTERM signal is sent to terminate all PID:

[linuxidc@linux:~/www.linuxidc.com] $sudo kill-9 5231 5207 5166 5131 5090

You can also use commands to replace expressions (…) To terminate the program with a command:

[linuxidc@linux:~/www.linuxidc.com] (pidof firefox)

Read here, this "how to use Linux pidof command" article has been introduced, want to master the knowledge of this article also need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.

Tags: Command program name process run in progress output two articles different content parameters examples scripts release processing proper special same in use Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Redmi MySQL Docker Xiaomi