Open In App

pidof Command in Linux with Examples

pidof command is used to find out the process IDs of a specific running program. It is basically an identification number that is automatically assigned to each process when it is created. Syntax:

pidof [options] program1 program2 ... programN

Working with Pidof Command

1. To find pid of any process



pidof bash

Use the name of the program as input to the command and command produced bash process ID in the output. 2. To get only one pid of a program

pidof -s bash

By default, pidof displays all pids of named command or program. So to display only one process ID of the program we have to pass “-s” option with it. 3. To get pids of scripts



pidof -x bash

The pidof command does not display pids of shell/perl/python scripts. We use the “-x” option to find the process ids of shells running the named script called bash. 4. To limit output based on the root directory

pidof -c bash 

If we want that pidof returns only process ids that are running with the same root directory, we use “-c” command-line option. Note: This option is ignored for non-root users, as they are unable to check the current root directory of processes they do not own. 5. To omit processes

pidof -o 87223 bash

If we want to ignore or omit processes with that process id, we can use pidof command to this. This is useful to ignore calling shell or shell script or specific pid. Here, it says we want to find all pids of bash and omit pid #87223. So, we use the given code for that. 6. To find pid of a program and kill it

p=$(pidof chrome)
kill $p

In this, firstly we find all PIDs of chrome server and then kill it.

Article Tags :