views:

34

answers:

3

EDIT 1

This code is currently printing to the console. What I would like to do is get the data in a string (and stop the data going to the console) so I can parse it for specific information. I hope my question is clear. Please ask for clarification if needed :)


EDIT 2

Another alternative could be to print it to a file and then I could read from that file afterwards. Just don't want it printing to the console :)


Hello Lovely Intelligent Computer People!

Any help with this would be great.

So far I'm using NSTask to spawn a process:

NSTask *top_task;
top_task = [[NSTask alloc] init];
[top_task setLaunchPath: @"/usr/bin/top"];

NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-s", @"1",@"-l",@"3600",@"-stats",@"pid,cpu,time,command", nil];
[top_task setArguments: arguments];


NSPipe *pipe;
pipe = [NSPipe pipe];

[top_task setStandardInput:[NSPipe pipe]];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[top_task launch];

Now, how do I grab the resulting information, let us say, in the form of an NSString?

Thanks for all and any help! :)

A: 

You might do it like that:

[top_task waitUntilExit];
NSString *string = [[NSString alloc] initWithData:[file readDataToEndOfFile] encoding:NSUnicodeStringEncoding] autorelease];
Max Seelemann
@Max. This doesn't seem to be working :(
Eric Brotto
Oh, right. You have to add this: [top_task setStandardOutput: top_pipe];
Max Seelemann
@Max, Is 'waitUntilExit' waiting for the task to finish? If so, isn't that a bit odd since it is iterating every second for an hour? It's still not printing, by the way.
Eric Brotto
That's right: for never terminating apps like top this will not work. Use itaiferber's solution then. However if you use some different app that does exit, this may be a simpler solution.
Max Seelemann
+1  A: 

You set up a pipe for standard input for no apparent reason. I'd think you would want a pipe for standard output, and then read from that.

JWWalker
+3  A: 

To launch your timer, you would want to do something like this:

NSTask *topTask = [NSTask new];
[topTask setLaunchPath:@"/usr/bin/top"];
[topTask setArguments:[NSArray arrayWithObjects:@"-s", @"1", @"-l", @"3600", @"-stats", @"pid,cpu,time,command", nil]];

NSPipe *outputPipe = [NSPipe pipe];
[topTask setStandardOutput:outputPipe];
[topTask launch];

So far, the code is really similar to what you've written, but you're going to have to add some more code to it. In order to get data every time top updates, you have add this one line of code to the end:

[[outuputPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify];

That will send you an NSFileHandleReadToEndOfFileCompletionNotification (if you register for it) every time top updates with output, and you can query the notification object for the incoming data and convert it to an NSString like this:

NSString *outputString = [[[NSString alloc] initWithData:[[notification userInfo] objectForKey:NSFileHandleNotificationDataItem] encoding:NSUTF8StringEncoding] autorelease];

You can read up on this in the NSFileHandle documentation, and Interacting With the Operating System.

itaiferber