pidof is a Linux command-line utility that returns the process IDs of a specific running program by its name.
Those IDs are printed on the standard output. A process ID is a unique identifier assigned to each process when it is created on the system.
The basic sysntax of pidof is as follows:
$ pidof [OPTIONS] PROGRAM_NAME
This command takes zero or more program names as parameters, but usually, only one program name is passed to it.
pidof will show the PIDs of all running programs that match the given name when executed without any options. For example, to find the PID of bash shell:
$ pidof bash
If any processes with names that match bash are running, their PIDs will be shown on the screen. The output will be empty if no matches are detected.
By default, all PIDs of matching running applications are displayed. To force pidof to display only one process ID use the -s option. Here is an example:
$ pidof -s bash
Now that you understand what the pidof command does and how to use it, let's look at a practical example of using pidof in conjunction with other commands.
Assume the firefox program is consuming excessive memory and has become unresponsive, and you need to terminate the processes. To begin, use pidof to find the PIDs:
$ pidof firefox
The above command will display all the process IDs associated with the flameshot program.
Once you've identified the flameshot process IDs, use the kill command to send a signal (SIGTERM) to terminate the processes.
$ kill 42322
Alternatively, you can use the command-line substitution $(...) or '.....' to find the process IDs and then terminate them in a simple one-liner.
$ kill -9 `pidof firefox`
Or another way:
$ kill -9 $(pidof firefox)
You can learn more about command substitution here.
Summing Up
This information should be enough to help you find the process IDs of programs using the pidof
command and terminate them when necessary.
That's all! Thank you for getting this far. I hope you find this digest useful.