LinuxCommandLibrary

psgrep

Find processes matching a given pattern

TLDR

Find process lines containing a specific string

$ psgrep [process_name]
copy

Find process lines containing a specific string, excluding headers
$ psgrep -n [process_name]
copy

Search using a simplified format (PID, user, command)
$ psgrep -s [process_name]
copy

SYNOPSIS

ps <>ps_options> | grep <>grep_options> <>search_pattern>

Examples:
ps -ef | grep httpd
ps aux | grep -i nginx | grep -v grep

PARAMETERS

ps options:
    Options passed to the ps command to control the format and content of the process list.

-e
    Select all processes.

-f
    Display full-format listing (UID, PID, PPID, C, STIME, TTY, TIME, CMD).

a
    Show processes for all users, including those not associated with a terminal.

u
    Display user-oriented format (USER, PID, %CPU, %MEM, VSZ, RSS, TTY, STAT, START, TIME, COMMAND).

x
    Show processes without a controlling TTY.

grep options:
    Options passed to the grep command to refine the search for the pattern.

-i
    Ignore case distinctions in both the pattern and the input data.

-v
    Invert the sense of matching, to select non-matching lines.

-w
    Select only those lines containing matches that form whole words.

-E
    Interpret pattern as an extended regular expression.

-P
    Interpret pattern as a Perl regular expression (highly expressive, non-standard).

search_pattern:
    The regular expression or literal string to search for within the process list.

DESCRIPTION

The term psgrep refers not to a standalone command, but to a ubiquitous Unix/Linux idiom combining the output of the ps command with the filtering capabilities of grep.

Its primary purpose is to quickly locate and display information about specific processes running on a system, typically by searching for a part of their name, a user, or other attributes available in the ps output. This method is incredibly versatile, allowing users to find processes related to a particular application, identify runaway processes, or confirm if a service is actively running.

The most common forms involve piping the full process list (e.g., from `ps -ef` or `ps aux`) into grep, followed by a search pattern. A common refinement is to use `grep -v grep` to exclude the grep command itself from the search results, preventing it from appearing as a matched process.

CAVEATS

psgrep is an idiom, not a single command, meaning its behavior and options are derived from the underlying ps and grep commands. It's often less efficient than dedicated tools like pgrep for simple process ID lookups.

A common pitfall is matching the grep command itself, which can be avoided by adding `| grep -v grep` to the pipeline. Output format is entirely dependent on the ps options used.

WHY `GREP -V GREP`?

When you execute `ps -ef | grep myprocess`, the grep command itself is a process running that contains the string 'myprocess' (or whatever you searched for). Therefore, the output would often include the grep command itself. Appending `| grep -v grep` filters out any line containing the string 'grep', effectively hiding the grep command from your results and showing only the actual target processes.

For example, `ps -ef | grep ssh` might show:
user 1234 1 0 10:00 ? 00:00:01 /usr/sbin/sshd -D
user 5678 1234 0 10:05 pts/0 00:00:00 grep ssh


Using `ps -ef | grep ssh | grep -v grep` would only show:
user 1234 1 0 10:00 ? 00:00:01 /usr/sbin/sshd -D

ALTERNATIVES TO PSGREP

While psgrep is powerful, for simply finding process IDs based on name, pgrep is often a more direct and efficient tool. For example, `pgrep nginx` will directly return the PIDs of all processes named 'nginx'. Similarly, pkill can be used to send signals to processes by name. These dedicated commands are generally preferred for scripting due to their precision and less verbose output.

HISTORY

The psgrep idiom emerged organically from the necessity to filter verbose process listings. As Unix-like systems became more prevalent, the combination of ps for process status and grep for pattern matching became a fundamental skill for system administrators and developers. While no specific development history exists for 'psgrep' itself, its enduring popularity highlights the power of Unix's philosophy of chaining small, specialized tools. The later introduction of commands like pgrep and pkill offered more streamlined and dedicated solutions for process searching and signaling, but psgrep remains a widely used, flexible, and powerful method.

SEE ALSO

ps(1), grep(1), pgrep(1), pkill(1), top(1), htop(1)

Copied to clipboard