35

I was looking for the best way to find the number of running processes with the same name via the command line in Linux. For example if I wanted to find the number of bash processes running and get "5". Currently I have a script that does a 'pidof ' and then does a count on the tokenized string. This works fine but I was wondering if there was a better way that can be done entirely via the command line. Thanks in advance for your help.

1
  • Is there a way to get a rolling count, especially of the number of tasks which match a specific string? Something like top | grep TaskName | wc -l Commented Dec 8, 2022 at 16:42

10 Answers 10

74

On Linux systems (and perhaps some non-Linux systems as well) that have pgrep available, the -c/--count option returns a count of the number of processes that match the given name:

pgrep --count command_name

If pgrep is not available, you may be able to use ps and wc. Again this may be somewhat system-dependent, but on typical Linux systems the following command will get you a count of processes:

ps -C command_name -e --no-headers | wc -l

The -C option to ps takes command_name as an argument, and the program prints a table of information about processes whose executable name matches the given command name. -e causes it to search processes owned by all users, not just the calling user. The --no-headers option suppresses the headers of the table, which are normally printed as the first line. With --no-headers, you get one line per process matched. Then wc -l counts and prints the number of lines in its input.

There are a few key differences between these commands:

  • pgrep uses grep-style matching, so e.g. pgrep sh will also match bash processes. If you want an exact match, also use the -x/--exact option. On the other hand, ps -C uses exact matching, not grep-style.
  • ps will output a row for itself and/or any other process it's being piped to, if those match the process selection criteria you gave. So ps -C ps --no-headers | wc -l and ps -C wc --no-headers | wc -l will always output at least 1, because they are counting themselves. On the other hand, pgrep excludes itself. So pgrep --count pgrep will output 0 unless there are other separate pgrep processes running. (Typically the pgrep behavior is what you want, which is why I generally recommend using pgrep if it's available.)

Here's a summary of a few variations:

Use case pgrep ps
Exact match pgrep -x -c command_name ps -C command_name -e --no-headers | wc -l
Substring match pgrep -c command_name N/A
Only current user pgrep -U "$UID" -c command_name ps -C command_name --no-headers | wc -l

There are many more possibilities, too many to list here. Check the man pages for pgrep and ps if you need different behavior, and you might find an option that implements it.

Sign up to request clarification or add additional context in comments.

6 Comments

Wiping out the headers is good for some reason, when I run ps there's 2 processes and when I run ps --no-headers | wc -l there's 3 processes. It seems to count the initial newline?
Hmm, looks like pgrep -c is not an option on OpenBSD / Darwin. Could you add a little explanation of why you've selected those flags ( for both commands ) ? Without some explanation it makes it tough for users on a different distro to even use a man page to try and translate your intentions...
Simple ps --no-headers | wc -l gives me 4 instead of 3 processes (that you'd see under ps --no-headers). What might be the case here?
@krzemian hard to say without seeing what the processes are.
@DavidZ Try ps >> test, then see if wc -l test and ps | wc -l give the same results. In my case they differ by 1, which bugs me (like a lot).
|
13
result=`ps -Al | grep command-name | wc -l`
echo $result

Comments

8
ps -Al | grep -c bash

Comments

6

You can try :

ps -ef | grep -cw [p]rocess_name

OR

ps aux | grep -cw [p]rocess_name

For e.g.,:

ps -ef | grep -cw [i]nit

1 Comment

I like how simple and clean this solution is
3

Some of the above didn't work for me, but they helped me on my way to this.

ps aux | grep [j]ava -c

For newbies to Linux:

ps aux prints all the currently running processes, grep searches for all processes that match the word java, the [] brackets remove the process you just ran so it wont include that as a running process and finally the -c option stands for count.

Comments

3

List all process names, sort and count

ps --no-headers -A -o comm | sort | uniq -c

You also can list process attached to a tty

ps --no-headers a -o comm | sort | uniq -c

You may filter with:

ps --no-headers -A -o comm | awk '{ list[$1] ++ } END { for (i in list) { if (list[i] > 10) printf ("%20s: %s\n", i, list[i]) } }'

Comments

2

ps aux | wc -l

This command shows number of processes running on the system by all the users.

For a specific user you can use the following command:

ps -u <username> | wc -l

replace with the actual username before running :)

Comments

1

Following bash script can be run as a cron job and you can possibly get email if any process forks itself too much.

for i in `ps -A -o comm= --sort=+comm | uniq`; 
do 
    if (( `ps -C $i --no-headers | wc -l` > 10 )); then 
        echo `hostname` $i `ps -C $i --no-headers | wc -l` ;
    fi
done

Replace 10 with your number of concern.

TODO: "10" could be passed as command line parameter as well. Also, few system processes can be put into exception list.

Comments

1

You can use ps(will show snapshot of processes) with wc(will count number of words, wc -l option will count lines i.e. newline characters). Which is very easy and simple to remember.

ps -e | grep processName | wc -l

This simple command will print number of processes running on current server. If you want to find the number of process running on current server for current user then use -U option of ps.

ps -U root | grep processName | wc -l

change root with username.

But as mentioned in lot of other answers you can also use ps -e | grep -c process_name which is more elegant way.

Comments

0

ps -awef | grep CAP | wc -l

Here "CAP" is the word which is in the my Process_Names.

This command output = Number of Processes + 1

This is why When we are running this command , our system read thats "ps -awef | grep CAP | wc -l " is also a process.

So yes our real answer is (Number of Processes) = Command Output - 1

Note : These processes are only those processes who include the name of "CAP"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.