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.
相关问题
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- Stop child process when parent process stops
- Error building gcc 4.8.3 from source: libstdc++.so
- Program doesn’t terminate when using processes
You can try :
ps -ef | grep -cw [p]rocess_name
OR
ps aux | grep -cw [p]rocess_name
eg:
ps -ef | grep -cw [i]nit
You can use
ps
(will show snapshot of processes) withwc
(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 ofps
.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.Some of the above didn't work for me, but they helped me on my way to this.
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.On systems that have
pgrep
available, the-c
option returns a count of the number of processes that match the given nameNote that this is a
grep
-style match, not an exact match, so e.g.pgrep sh
will also matchbash
processes. If you want an exact match, also use the-x
option.If
pgrep
is not available, you can useps
andwc
.The
-C
option tops
takescommand_name
as an argument, and the program prints a table of information about processes whose executable name matches the given command name. This is an exact match, notgrep
-style. 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. Thenwc -l
counts and prints the number of lines in its input.List all process names, sort and count
You also can list process attached to a tty
You may filter with:
I don't know what it is on other distros, but on Ubuntu, it's:
nproc
is part ofcoreutils
. So, it should be available on most linux distros.