I am trying to get the following CLI command to work in python.
pgrep fmserverd fmserver_helperd fmslogtrimmer fmxdbc_listener | wc -l
A return of 4 tells me that all 4 processes are running. This works fine in the CLI but does not function correctly in Python. I am doing the following with just one of the processes from the command line:
import subprocess
print subprocess.check_output ([ 'pgrep', 'fmserver_helperd', '|', 'wc', '-l'], shell=True, stderr=subprocess.PIPE)
or
print subprocess.check_output ([ 'pgrep', 'fmserver_helperd', '|', 'wc', '-l'], shell=True)
Which returns:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 573, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['pgrep', 'fmserver_helperd', '|', 'wc', '-l']' returned non-zero exit status 2
If I write a program in a file I basically get the same result. This code protects check_output with a try catch block and the output looks like this:
sp.check_output: ['pgrep', 'fmserver_helperd', '|', 'wc', '-l'] ro: True sh: True out:
usage: pgrep [-Lfilnoqvx] [-d delim] [-F pidfile] [-G gid]
[-P ppid] [-U uid] [-g pgrp]
[-t tty] [-u euid] pattern ...
DoSubProcess exception: Command '['pgrep', 'fmserver_helperd', '|', 'wc', '-l']' returned non-zero exit status 2
I am really stuck on this and would really appreciate your help. TY