The pidof
command is used to find the process ID (PID) number of the process with the specified name. This is useful for identifying and managing processes running on a Linux system.
Overview
The basic syntax of the pidof
command is as follows:
pidof [OPTIONS] name
where name
is the name of the process for which the PID is to be found.
For example, to find the PID of the apache2
process, run the following command:
pidof apache2
The output would be a list of PIDs separated by spaces, like this:
1234 5678 9012
This means that there are three instances of the apache2
process running, with PIDs 1234, 5678, and 9012.
The pidof
command can also be used in scripts to perform various actions on processes. For example, the following command kills all instances of the apache2
process:
kill $(pidof apache2)
This command uses command substitution to pass the output of pidof
to the kill
command, which sends a SIGTERM signal to each process.
Options
The following table lists the available options for the pidof
command:
Option | Description |
---|---|
-c | Only display the number of PIDs found |
-x | Only match processes with the exact name specified |
-s | Only match processes in the same session as the PID of the calling process |
-o | Only match processes with the same effective user ID as the PID of the calling process |
-n | Only match processes with the same name as the PID of the calling process |
Troubleshooting tips
- If
pidof
returns no output, it means that the specified process is not running. - If
pidof
returns multiple PIDs, it means that there are multiple instances of the specified process running. - If
pidof
returns an error message, check that the process name is spelled correctly and that the process is running.
Notes
- The
pidof
command is a simple and useful tool for managing processes on a Linux system. - The command can be used in scripts and other automation tools to perform various actions on processes.
- The
pidof
command is typically installed by default on most Linux distributions.