 |
 |
parsing ps ax
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jun 2000
Status:
Offline
|
|
I am executing a task using Cocoa to retrieve the output of the "ps ax" command so that I can parse out what programs are running but the path that is read into my program is only 53 characters long and it cuts off the remainder of where the program is located. I am thinking that when I run "sh -c ps ax" it reads in a default terminal window size and only outputs what will fit in that window size. Does anyone know a command for sh that would change what it thinks is the window size or knows of another solution.
My code is as follows. (note: I used ManOpen's source code to figure out how to execute a command and read in the resulting text)
- (void)callpsax
{
NSString *command = [NSString stringWithString:@"ps ax"];
[taskData release];
taskData = nil;
taskData = [[self dataByExecutingCommand:command] retain];
command = [NSString stringWithCString:[taskData bytes] length:[taskData length]]; //  if you break after this line and view the contents of command you will see any long path names cut off when they reach 53 characters.
[self parseOutput:[NSString stringWithCString:[taskData bytes] length:[taskData length]]];
}
- (NSData *)dataByExecutingCommand  NSString *)command
{
NSPipe *pipe = [[NSPipe alloc] init];
NSTask *task = [[NSTask alloc] init];
NSData *output;
[task setLaunchPath:@"/bin/sh"];
[task setArguments:[NSArray arrayWithObjects:@"-c", command, nil]];
[task setStandardOutput  ipe];
[task setStandardError:[NSFileHandle fileHandleWithNullDevice]]; //don't care about output
[task launch];
output = [[pipe fileHandleForReading] readDataToEndOfFile];
[task waitUntilExit];
[pipe release];
#ifdef OPENSTEP
[[task arguments] release]; //NeXT bug
#endif
[task release];
return output;
}
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Apr 2001
Location: San Francisco, USA
Status:
Offline
|
|
ps axc
This 'c' option shows process names instead of complete paths.
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Apr 2001
Location: San Francisco, USA
Status:
Offline
|
|
You might be able to peek at the process table itself using sysctl(). This would be much faster than executing ps and parsing the output.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jun 2000
Status:
Offline
|
|
I tried the ps axc and it cuts off the name of the program after 15 characters even if you just run the command in the Terminal window. I also noticed that the program name when I use "ps axc" instead of "ps ax" can be different. The specific program that I noticed this was Stuffit Expander for OS X. When I use "ps axc" it reports the name as LaunchCFMApp but it reports the name properly if I use "ps ax". IE also reports as LaunchCFMApp with the "ps axc" command. I think that it is a Carbon thing. It makes me wonder where the command is getting the process information from if it is reporting it so differently.
-Brian
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Apr 2001
Status:
Offline
|
|
If you insist on using `ps`, use the -w option, like `ps axw' to get more than 15 characters. From the man pages:
<font face = "courier">
-w Use 132 columns to display information, instead of the default
which is your window size. If the -w option is specified more
than once, ps will use as many columns as necessary without re-
gard for your window size.
</font>
Also, you can use -o option to get only the information you want, like just the process information. Check <font face = "courier">man 1 ps</font> for more information.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jun 2000
Status:
Offline
|
|
The ps axww worked well. Thanks. I had tried it with ps axw and it hadn't made enough of a difference. I have a corollary question that is in two parts. I would like to more directly access the process name, path and pid and I was considering two possible methods. The first was to find the source code for the ps command and using the c code directly in my program to access the correct data. In this case I would have to find the source code so my question is... Does anyone know where to find the source code for basic unix commands? The other possibility is using sysctl() which honeydew recommended to retrieve this information. I didn't find much documentation on sysctl() and while I found the .h file I am not sure if this would be the sanctioned/best/simplest way. Has anyone used sysctl() to retrieve this type of information and has source code that I could look at to understand it better? Email me directly if you prefer.
-Brian
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Apr 2001
Location: San Francisco, USA
Status:
Offline
|
|
man 3 sysctl
This is my first time using sysctl. I read the man page and threw something together. Hopefully your code will look better than mine! :o
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
#include <sys/param.h>
#include <sys/sysctl.h>
<font color = brown>/* Print PIDs of all running processes */</font>
<font color = green>int</font> main( <font color = green>void</font> )
{
<font color = green>int</font> i, mib[<font color = blue>3</font>];
size_t len;
<font color = brown>/* Allocate space <font color = green>for</font> the process table */</font>
<font color = green>struct</font> kinfo_proc ps[<font color = blue>128</font>];
len = <font color = green>sizeof</font>( ps );
<font color = brown>/* Path to the process table */</font>
mib[<font color = blue>0</font>] = CTL_KERN;
mib[<font color = blue>1</font>] = KERN_PROC;
mib[<font color = blue>2</font>] = KERN_PROC_ALL;
<font color = brown>/* Populate the table */</font>
sysctl( mib, <font color = blue>3</font>, ps, &len, <font color = green>NULL</font>, <font color = blue>0</font> );
<font color = brown>/* Figure out the number of processes returned */</font>
len /= <font color = green>sizeof</font>( <font color = green>struct</font> kinfo_proc );
<font color = brown>/* Iterate through all procs */</font>
<font color = green>for</font>( i = <font color = blue>0</font>; i<len; i++ )
{
printf( <font color = red>"%d\n"</font>, ps[i].kp_proc.p_pid );
}
<font color = green>return</font> <font color = blue>0</font>;
}
</font>[/code]
Edit: In my example, ps[i].kp_proc is a struct extern_proc. Take a look at <sys/proc.h> for details. Finding the executable name is going to be more difficult than just printing the PID, but hopefully it's in there somewhere. Good luck!
[ 06-06-2001: Message edited by: honeydew ]
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|