Recent

Author Topic: How could I get Process list with paths to executable?  (Read 4884 times)

Julius

  • New Member
  • *
  • Posts: 11
How could I get Process list with paths to executable?
« on: January 08, 2012, 02:03:21 pm »
Hello community,

I have to find a way to get list of PID's and paths to the executables they represent. On Windows I did it using CreateToolhelp32Snapshot procedure but it is for windows only. Are there any alternatives for Mac OS X ?

What I try to achieve is a list of Pid|Executable like 1234|/bin/someprogram

Of course I could parse the output of ps -A command, but is there another way? If there is no other way may someone share ones code of parsing ps ?

Thank you in advance!

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: How could I get Process list with paths to executable?
« Reply #1 on: January 08, 2012, 03:22:56 pm »
A while ago on the mailing list there was discussion regarding this subject on Linux which could be of interest: http://lists.lazarus.freepascal.org/pipermail/lazarus/2011-August/066036.html. The conclusion there was that parsing ps was probably the best way.

When parsing ps, make it as easy as possibly by carefully selecting the output format. Fe.
Code: [Select]
ps -o pid,comm
to show only pid and executable. The format is fixed and easy to parse with the copy function.
Code: [Select]
ps -o pid,command
will show pid and executable+parameters.
All keywords usable with ps -o are listed at the end of the man page for ps.

EDIT:
Code: [Select]
ps -o pid=,comm=will get rid of the header also.
« Last Edit: January 08, 2012, 03:27:13 pm by ludob »

 

TinyPortal © 2005-2018