how to find number of processes running particular command in python
Question: The output of

    
    
    ps uaxw | egrep 'kms' | grep -v 'grep'
    

yields:

    
    
    user1  8148  0.0  0.0 128988  3916 pts/8    S+   18:34   0:00 kms 
    user2 11782  0.7  0.3 653568 56564 pts/14   Sl+  20:29   0:01 kms
    

Clearly two processes running the program. I want to store this number (2
here) as a variable. Any suggestions on how to do this in python?

I tried the following:

    
    
    procs = subprocess.check_output("ps uaxw | egrep 'kmns' |grep -v 'grep'",shell=True)
    

But i get the following (I think when the jobs are not currently running, so
number of processes running the jobs is zero):

> Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python2.7/subprocess.py", line 573, in check_output raise CalledProcessError(retcode, cmd, output=output) subprocess.CalledProcessError: Command 'ps uaxw | egrep 'kmns' |grep -v 'grep'' returned non-zero exit status 1

How do I get around this?

Btw, here is the function I wrote to detect if my system was busy (which means
if the number of cpus > total installed, and if load avg > 0.9 per cpu):

    
    
    def busy():
        import subprocess
        output = subprocess.check_output("uptime", shell=False)
        words = output.split()
        sys.stderr.write("%s\n"%(output)) 
        procs = subprocess.check_output("ps uaxw | egrep '(kmns)' | grep -v 'grep'", shell=True)
        kmns_wrds = procs.split("\n")
        wrds=words[9]
        ldavg=float(wrds.strip(','))+0.8
        sys.stderr.write("%s %s\n"%(ldavg,len(kmns_wrds)))
        return max(ldavg, len(kmns_wrds)) > ncpus
    

The above is called by:

    
    
    def wait_til_free(myseconds):
        while busy():
            import time
            import sys
            time.sleep(myseconds)
            """ sys.stderr.write("Waiting %s seconds\n"%(myseconds)) """
    

which basically tells the system to wait while all cpus are taken.

Any suggestions?

Many thanks!


Answer: If you're going to do this all with a big shell command, just add the `-c`
argument to
[`grep`](http://pubs.opengroup.org/onlinepubs/009604499/utilities/grep.html),
so it gives you a count of lines instead of the actual lines:

    
    
    $ ps uaxw |grep python |grep -v grep
    abarnert         1028   0.0  0.3  2529488  55252 s000  S+    9:46PM   0:02.80 /Library/Frameworks/Python.framework/Versions/3.4/Resources/Python.app/Contents/MacOS/Python /Library/Frameworks/Python.framework/Versions/3.4/bin/ipython3
    abarnert         9639   0.0  0.1  2512928  19228 s002  T     3:06PM   0:00.40 /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python /usr/local/bin/ipython2
    $
    $ ps uaxw |grep python |grep -c -v grep
    2
    

Of course you could make this more complicated by adding a `| wc -l` to the
end, or by counting the lines in Python, but why?

* * *

Alternatively, why even involve the shell? You can search within Python just
as easily as you can run `grep`—and then you don't have the problem that
you've accidentally created a `grep` process that `ps` will repeat as matching
your search and then need to `grep -v` it back out:

    
    
    procs = subprocess.check_output(['ps', 'uaxw']).splitlines()
    kms_procs = [proc for proc in procs if 'kms' in proc]
    count = len(kms_procs)
    

* * *

Or, even more simply, don't ask `ps` to give you a whole bunch of information
that you don't want and then figure out how to ignore it, just ask for the
information you want:

    
    
    procs = subprocess.check_output(['ps', '-a', '-c', '-ocomm=']).splitlines()
    count = procs.count('kms')
    

* * *

Or, even more more simplierly, install
[`psutil`](https://pypi.python.org/pypi/psutil) and don't even try to run
subprocesses and parse their output:

    
    
    count = sum(1 for proc in psutil.process_iter() if proc.name() == 'kms')
    

