4

I'm trying to make a NSTask running a command like this:

ps -clx | grep 'Finder' | awk '{print $2}'

Here is my method

- (void) processByName:(NSString*)name {
    NSTask *task1 = [[NSTask alloc] init];
    NSPipe *pipe1 = [NSPipe pipe];
    [task1 waitUntilExit];
    [task1 setLaunchPath: @"/bin/ps"];
    [task1 setArguments: [NSArray arrayWithObjects: @"-clx", nil]];
    [task1 setStandardOutput: pipe1];

    NSTask *task2 = [[NSTask alloc] init];
    NSPipe *pipe2 = [NSPipe pipe];
    [task2 setLaunchPath: @"/usr/bin/grep"];
    [task2 setArguments: [NSArray arrayWithObjects: @"'Finder'", nil]];
    [task2 setStandardInput:pipe1];
    [task2 setStandardOutput: pipe2];

    NSTask *task3 = [[NSTask alloc] init];
    NSPipe *pipe3 = [NSPipe pipe];
    [task3 setLaunchPath: @"/usr/bin/grep"];
    [task3 setArguments: [NSArray arrayWithObjects: @"'{print $2}'", nil]];
    [task3 setStandardInput:pipe2];
    [task3 setStandardOutput: pipe3];

    NSFileHandle *file = [pipe3 fileHandleForReading];

    [task1 launch];
    [task2 launch];
    [task3 launch];

    NSData *data;
    data = [file readDataToEndOfFile];

    NSString *string;
    string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

    NSLog(@"Result: %@", string);
}

But the result is just

Result:

What am I doing wrong?

EDIT

- (void) processByName:(NSString*)name {
    NSTask *task1 = [[NSTask alloc] init];
    NSPipe *pipe1 = [NSPipe pipe];
    [task1 waitUntilExit];
    [task1 setLaunchPath: @"/bin/ps"];
    [task1 setArguments: [NSArray arrayWithObjects: @"-clx", nil]];
    [task1 setStandardOutput: pipe1];

    NSTask *task2 = [[NSTask alloc] init];
    NSPipe *pipe2 = [NSPipe pipe];
    [task2 setLaunchPath: @"/usr/bin/grep"];
    [task2 setArguments: [NSArray arrayWithObjects: @"'Finder'", nil]];
    [task2 setStandardInput:pipe1];
    [task2 setStandardOutput: pipe2];

    NSTask *task3 = [[NSTask alloc] init];
    NSPipe *pipe3 = [NSPipe pipe];
    [task3 setLaunchPath: @"/usr/bin/grep"];
    [task3 setArguments: [NSArray arrayWithObjects: @"'{print $2}'", nil]];
    [task3 setStandardInput:pipe2];
    [task3 setStandardOutput: pipe3];

    NSFileHandle *file = [pipe3 fileHandleForReading];

    [task1 launch];
    [task2 launch];
    [task3 launch];

    [[NSNotificationCenter defaultCenter] addObserverForName:NSTaskDidTerminateNotification
                                                      object:task3
                                                       queue:nil
                                                  usingBlock:^(NSNotification* notification){

                                                      NSData * data = [file readDataToEndOfFile];

                                                      NSString * string;
                                                      string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
                                                      NSLog(@"Result: %@", string);
                                                  }];
}
dhrm
  • 14,335
  • 34
  • 117
  • 183
  • Wouldn't it make more sense to launch the tasks after you have attached the observer? What if a task completes before the observer is attached? – Sentry.co Dec 16 '16 at 12:58

3 Answers3

5

The tasks run in a separate process from your code, i.e., asychronously. They probably haven't finished (they may not have even launched!) by the time you get to the readDataToEndOfFile two lines later.

If you're already on a background thread here, you can poll their status: while( ![task isRunning]){, or if you're on the main thread, I'd suggest using GCD to put this onto a queue and doing the polling there.

Actually, better than that would be to use notifications:

[task3 launch];

[[NSNotificationCenter defaultCenter] addObserverForName:NSTaskDidTerminateNotification
                                                  object:task3
                                                   queue:nil
                                              usingBlock:^{

    NSData * data = [file readDataToEndOfFile];

    NSString * string;
    string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

    NSLog(@"Result: %@", string);
}];

See TN2050: Observing Process Lifetime Without Polling. Each NSTask will send NSTaskDidTerminateNotification when it terminates (you should, ideally, check its return code rather than assuming it ran successfully). You can create a block to be run when task3 sends that notification.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • Could you please provide me with a small example showing how I can do this using GCD? – dhrm Feb 22 '12 at 18:36
  • You know, I just thought of a better way to do this. Please see my edit. – jscs Feb 22 '12 at 18:50
  • Please see my edit. I've added the code, but I still returns an empty string. – dhrm Feb 22 '12 at 19:05
  • Have you verified that the command string you're running does what you expect if you just enter it to the CL? I see two things that look funny: you're not using the argument to the method, and you're running `grep '{print $2}'` as the third task -- did you mean `awk`? – jscs Feb 23 '12 at 21:09
  • I cannot get the right result from the code "Edit" part – droughtrain May 07 '14 at 21:06
2

The following code works for me.

NSTask *task1 = [[NSTask alloc] init];
NSPipe *pipe1 = [NSPipe pipe];
[task1 waitUntilExit];
[task1 setLaunchPath: @"/bin/sh"];
[task1 setArguments: [NSArray arrayWithObjects: @"-c",@"ps -A |grep -m1 Finder | awk '{print $1}'", nil]];
[task1 setStandardOutput: pipe1];
[task1 launch];

NSFileHandle *file = [pipe1 fileHandleForReading];
NSData * data = [file readDataToEndOfFile];

NSString * string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Result: %@", string);
droughtrain
  • 257
  • 1
  • 4
  • 18
0

Almost 8 years later, I think grep binary is'nt in /usr/bin, for me it's in /bin. Also, for your awk command, you set the launch path to grep.

xelahot
  • 11
  • 3