Linux / Bash, using ps -o to get process by specific name?

LinuxBashProcess

Linux Problem Overview


I am trying to use the ps -o command to get just specific info about processes matching a certain name. However, I am having some issues on this, when I try to use this even to just get all processes, like so, it just returns a subset of what a normal ps -ef would return (it doesn't return nearly the same number of results so its not returning all running processes)

ps -ef -o pid,time,comm

I want to try something like this (below) but incorporate the ps -o to just get specific info from it (just the PID)

ps -ef |grep `whoami`| grep firefox-bin

Any advice is appreciated as to how to do this properly, thanks

Linux Solutions


Solution 1 - Linux

This will get you the PID of a process by name:

pidof name

Which you can then plug back in to ps for more detail:

ps -p $(pidof name)

Solution 2 - Linux

This is a bit old, but I guess what you want is: ps -o pid -C PROCESS_NAME, for example:

ps -o pid -C bash

EDIT: Dependening on the sort of output you expect, pgrep would be more elegant. This, in my knowledge, is Linux specific and result in similar output as above. For example:

pgrep bash

Solution 3 - Linux

ps -fC PROCESSNAME

ps and grep is a dangerous combination -- grep tries to match everything on each line (thus the all too common: grep -v grep hack). ps -C doesn't use grep, it uses the process table for an exact match. Thus, you'll get an accurate list with: ps -fC sh rather finding every process with sh somewhere on the line.

Solution 4 - Linux

Sometimes you need to grep the process by name - in that case:

ps aux | grep simple-scan

Example output:

simple-scan  1090  0.0  0.1   4248  1432 ?        S    Jun11   0:00

Solution 5 - Linux

Sorry, much late to the party, but I'll add here that if you wanted to capture processes with names identical to your search string, you could do

pgrep -x PROCESS_NAME

> -x Require an exact match of the process name, or argument list if -f is given. The default is to match any substring.

This is extremely useful if your original process created child processes (possibly zombie when you query) which prefix the original process' name in their own name and you are trying to exclude them from your results. There are many UNIX daemons which do this. My go-to example is ninja-dev-sync.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRickView Question on Stackoverflow
Solution 1 - LinuxAlex HowanskyView Answer on Stackoverflow
Solution 2 - Linuxh7rView Answer on Stackoverflow
Solution 3 - LinuxGerald HughesView Answer on Stackoverflow
Solution 4 - Linuxuser3751385View Answer on Stackoverflow
Solution 5 - LinuxSpadeView Answer on Stackoverflow