Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 09-18-2006, 02:41 PM   #1
Registered: Oct 2005
Location: Tierra Firma, Earth
Distribution: Debian of course...
Posts: 198

Rep: Reputation: 30
how to count the number of occurrences of a process

I WAS going to ask how to do this but since I figured it out I present it to fellow n00bs(and myself in case I forget).

I wanted to know how many occurrences of a process that I have running--

command: ps -aux|grep 'some_process'|wc -l
explanation: 1)take the current listing of all processes in BSD format 2)search for all lines containing 'some_process' 3)then count how many lines 'some_process' occurs on.

Is there a better way to do this?
Old 09-18-2006, 02:42 PM   #2
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
pidof <process name> | wc -w
Old 09-18-2006, 03:31 PM   #3
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,474
Blog Entries: 1

Rep: Reputation: 67
variations about this theme....

$ pgrep <process_name> | wc -l

Last edited by marozsas; 09-18-2006 at 03:35 PM.
Old 09-18-2006, 04:17 PM   #4
Senior Member
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,285

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Originally Posted by beeblequix
ps -aux|grep 'some_process'|wc -l
This method can come up with a count that's one too hig (intermittantly). You will sometimes catch your "grep" in the count to!

I know I'm only running one copy of "init", but see below where my count would have been two by your method:
$ ps aux | grep init
root         1  0.0  0.0  1600  536 ?        S    Sep14   0:02 init [2]
haertig  22987  0.0  0.0  1928  600 pts/0    R+   15:13   0:00 grep init


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
count total number of lines in several files xushi Programming 5 11-12-2005 04:42 PM
Javasript to count number of mouse clicks LinuxLala Programming 3 06-17-2005 12:23 AM
C++ STL count() number of substring occurences... nyk Programming 4 06-25-2004 07:12 AM
help on finding the number that has max occurrences in a column [sql query] zeppelin Programming 6 06-15-2004 01:41 PM
Count number of line in a file. philipina Programming 7 03-18-2004 05:04 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:23 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration