0

I'm trying to get the pid with the help of process name. i have tried this solution. But it gives me this error

    Traceback (most recent call last):
  File "pidName.py", line 10, in <module>
    getPIDs("safari")
  File "pidName.py", line 4, in getPIDs
    pidlist = map(int, s.check_output(["pidof", process]).split())
  File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 567, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1343, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

and the code is this :

import subprocess as s
def getPIDs(process):
    try:
        pidlist = map(int, s.check_output(["pidof", process]).split())
    except  s.CalledProcessError:
        pidlist = []
    print 'list of PIDs = ' + ', '.join(str(e) for e in pidlist)

if __name__ == '__main__':
    getPIDs("safari")

Since, i'm new to these things. I'm unable to solve this problem. But in the solution it works. But somehow it doesn't on my Mac. I'm using mac. and running the python script from terminal.

which python gives me this result:-

/usr/local/bin/python

so, i'm using the inbuilt python version i think.

can anyone help me with this problem.

Community
  • 1
  • 1
Krishna Kumar
  • 17
  • 1
  • 8
  • does `pidof safari` work if you run it from the terminal manually? – FamousJameous Apr 17 '17 at 20:13
  • The command you want to use is probably `pgrep` (potentially with option `-i` to not care of capitalization of the processes) rather than pidof. – JohanL Apr 17 '17 at 20:32
  • @FamousJameous i tried that. it also gave me the same error. @JohanL can you please explain how to use `pgrep` ? – Krishna Kumar Apr 18 '17 at 14:04
  • It looks like OSX doesn't provide the `pidof` command (and potentially also the `pgrep` command, although I don't have a Mac so I could check). Take a look at this SO question and see if it helps: http://stackoverflow.com/q/11546765/3901060 – FamousJameous Apr 18 '17 at 14:18

0 Answers0