LinuxCommandLibrary

pidof

Find process ID(s) by name

TLDR

List all process IDs with given name

$ pidof [bash]
copy

List a single process ID with given name
$ pidof -s [bash]
copy

List process IDs including scripts with given name
$ pidof -x [script.py]
copy

Kill all processes with given name
$ kill $(pidof [name])
copy

SYNOPSIS

pidof [-s] [-c] [-x] [-o <pid>] [-m] [-q] [-p <dirname>] <program_name>...

PARAMETERS

-s
    Return only one pid. This is the default behavior.

-c
    Only return process ids that run with the same root directory.

-x
    Also return process ids of processes running scripts given as arguments.

-o <pid>
    Omit the given pid. The special pid %PPID% can be used to name the parent process, that means the pidof process itself.

-m
    Only return process ids that run with same name as given argument.

-q
    Suppress error messages.

-p <dirname>
    Examine only processes which have files in <dirname> open.

<program_name>
    The name of the program to find the PID(s) for. Multiple program names can be specified.

DESCRIPTION

The pidof command finds the process ID(s) of running programs. It searches the process table for program names that match the name(s) given as arguments. By default, it only returns a single PID for each program name, representing the oldest process found. The command can also return all PIDs belonging to the same program name.

pidof is commonly used in shell scripts to check if a process is running and to obtain its PID for further actions like sending signals (e.g., kill). It provides a convenient way to automate process management tasks.

CAVEATS

The pidof command relies on the program name matching the command name in the process table. Programs that change their name after execution might not be found correctly. Also, if multiple programs have the same name, pidof will return their PIDs (or one PID with the `-s` option).

SIGNAL HANDLING

pidof itself doesn't send signals. However, the output of pidof (the process ID) is often piped to the kill command to send signals to processes, allowing for controlled termination or other actions.

HISTORY

pidof has been a standard utility in various Linux distributions for many years. Its purpose is to provide a simple and reliable way to obtain PIDs, mainly useful in shell scripting and system administration tasks. It evolved to simplify process management automation.

SEE ALSO

ps(1), kill(1), pgrep(1), pkill(1)

Copied to clipboard