Master the Linux pidof Command: A Comprehensive Guide

Peter Hou
2 min readJun 7, 2023

This guide provides a comprehensive understanding of the pidof command in Linux. From its history to use-cases, common parameters, and the most efficient methods to use it, the guide is designed to equip Linux beginners with the necessary skills to use this command effectively.

Instructions

This guide aims to provide beginners with a comprehensive understanding of the pidof command in Linux. We will delve into its history, use-cases, common parameters, usage methods, and related tips and tricks.

History

The pidof command is part of the SysVinit package, and it's widely used in Unix-like operating systems. pidof finds the process ID of a running program, which is crucial for process management in Linux.

When and why to use it

pidof command is used when there is a need to find the process ID (PID) of a running program. This can be very useful for process management, system monitoring, and when you need to terminate a specific process using its PID.

How to use it

To use the pidof command, simply type pidof followed by the name of the process you're interested in.

$ pidof processname
12345

The commonly used parameters

  • -s This parameter is used to only return the first PID found.
$ pidof -s processname
12345
  • -c This parameter checks whether the process is running in the same root. This can be helpful when running in a chroot environment.
$ pidof -c processname
No output if not found in the same root

Other supported parameters

  • -x Consider processes that are scripts.
  • -o Omit PIDs, this can be useful when you want to exclude certain PIDs.

Most common use cases

The most common use case of pidof is when you need to kill a process, and you use pidof to find the PID of that process.

$ kill -9 $(pidof processname)

The tricky skills

A trick with pidof is that you can use it in a script to check if a certain process is running, and take action based on that. Here's a small script snippet that shows how to do it.

$ if pidof processname > /dev/null
> then
> echo "Process is running"
> else
> echo "Process is not running"
> fi
Process is running

What needs to be noted

  • The pidof command might not be present on all Linux distributions, as some may use pgrep or other similar commands.
  • Always ensure you have the correct permissions to view the process ID, especially for system processes.

Conclusion

Understanding the pidof command is important for effective process management in Linux. Whether you need to kill a process, check if a process is running, or monitor system activity, pidof is an essential tool in your Linux toolkit.

--

--

Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.