
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]
pidof command details | |
---|---|
Description | Get the process ID of arunning program |
Category | Processes Management |
Difficulty | Easy |
Root privileges | No |
Estimated completion time | 5m |
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.
Purpose
Find the process ID of a running program by name on Linux.
Syntax
The basic syntax is as follows:
pidof command
OR
pidof program
OR
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):
2427 2426 2424 2423 2418 2387 2386 2385 2384 2383 2241 2240 2238 2237 2223
To display only one pid of the program pass the -s option as follows:
$ pidof -s php5-cgi
Sample outputs (see fig.01):
2427
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
Omit processes
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 server list=$(pidof -o %PPID lighttpd) # .. now do something on all pids stored in $list for p in $list do echo "Killing $p..." kill -TERM $p done |
Find pid of a program and kill it
In this example, find all PIDs of nginx server and kill it:
p=$(pidof nginx) kill $p |
Find pids of firefox and kill it focefully as the browser has stuck/frozen using kill command
p=$(pidof firefox) kill -9 $p |
pidof command options
From the pidof(8) command man page:
Option | Description |
---|---|
-s | Find only one pid i.e. single shot. |
-c | 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. |
-n | Avoid stat(2) system function call on all binaries which are located on network based file systems like NFS. |
-x | Find pid for scripts too. |
-o | Tells pidof to omit processes with that process id. |
See also
- pidof(8) Linux/Unix command man page
|