Linux pidof Command Examples To Find PID of A Program/Command
I am a new Linux system user and I using CentOS Linux on server. How can I find the process ID (PID) of a running program on Linux based system using shell prompt?
A PID is an acronym for process identification number on Linux or Unix-like systems. It is nothing but an identification number that is automatically assigned to each process when it is created.[donotprint]
You can use pidof utility to find the process id’s (PIDs) by name. Please note that the pidof command only works on Linux based system, Unix user either try ps command or pgrep command to find the pid of a running program.
Find the process ID of a running program by name on Linux.
The basic syntax is as follows:
pidof [options] program1 program2 ... programN
NOTE: You may need to run this program as root user to find and kill pid of privileged programs.
pidof command examples
To find pid of lighttpd process, enter: $ pidof lighttpd Sample outputs:
Get only one pid of a program named php5-cgi
By default pidof displays all pids of named command/program: $ pidof php5-cgi Sample outputs (see fig.01):
To display only one pid of the program pass the -s option as follows: $ pidof -s php5-cgi Sample outputs (see fig.01):
Get pids of scripts
The pidof command will not display pids of shell/perl/python scripts. To find the process id’s of shells running the named script called featch-data, pass the -x option: $ pidof -x fetch-data
You can ask pidof commmand to ignore or omit processes with that process id. This is useful to ignore calling shell or shell script or specific pid. In this example, find all pids of lighttpd but omit pid # 4242: # pidof -o 4242 lighttpd You can use the special pid %PPID to name the parent process of the pidof program in shell scripts:
#!/bin/bash# please ignore our pid and get all pids of lighttpd on serverlist=$(pidof-o%PPID lighttpd)# .. now do something on all pids stored in $listfor p in$listdoecho"Killing $p..."kill-TERM$pdone
# please ignore our pid and get all pids of lighttpd on server
list=$(pidof -o %PPID lighttpd) # .. now do something on all pids stored in $list
for p in $list
echo "Killing $p…"
kill -TERM $p
Find pid of a program and kill it
In this example, find all PIDs of nginx server and kill it:
Find pids of firefox and kill it focefully as the browser has stuck/frozen using kill command
kill -9 $p
pidof command options
From the pidof(8) command man page:
Find only one pid i.e. single shot.
Only return process ids that are running with the same root directory. This option is ignored for non-root users, as they will be unable to check the current root directory of processes they do not own.
Avoid stat(2) system function call on all binaries which are located on network based file systems like NFS.
Find pid for scripts too.
Tells pidof to omit processes with that process id.
The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.