“pidof” is a Unix/Linux command that is used to find the process ID (PID) of a running process based on its name. This can be useful in several scenarios such as:
- Checking if a process is running: You can use the “pidof” command to check if a process is running. For example, if you want to check if the Apache web server is running, you can use the following command: pidof apache2. If the Apache web server is running, the command will output its PID.
- Killing a process: Once you have the PID of a process, you can use the “kill” command to stop the process. For example, if you want to stop the Apache web server, you can use the following command: kill $(pidof apache2).
- Monitoring a process: The “pidof” command can be used in combination with other tools to monitor a process. For example, you can use the “top” command to monitor the CPU and memory usage of a process by using its PID.
It’s important to note that the “pidof” command only returns the PID of the first instance of the process it finds. If there are multiple instances of the same process running, you need to use other tools to find all of their PIDs.
If you encounter the below error while running the command pidof:
pidof: command not found
you may try installing the below package as per your choice of distribution:
|Debian||apt-get install sysvinit-utils|
|Ubuntu||apt-get install sysvinit-utils|
|Arch Linux||pacman -S procps-ng|
|Kali Linux||apt-get install sysvinit-utils|
|CentOS||yum install sysvinit-tools|
|Fedora||dnf install procps-ng|
|OS X||brew install pidof|
|Raspbian||apt-get install sysvinit-utils|
pidof Command Examples
1. List all process IDs with given name:
# pidof bash
2. List a single process ID with given name:
# pidof -s bash
3. List process IDs including scripts with given name:
# pidof -x script.py
4. Kill all processes with given name:
# kill $(pidof name)