1

I use a NSTask as follows:

    NSTask *task;
    task = [[NSTask alloc] init];

    [task setLaunchPath:@"/bin/bash"];

    NSMutableString *script = ... // actual script here, works fine

    NSArray *args = [NSArray arrayWithObjects:@"-l",
                     @"-c",
                     script,
                     nil];

    [task setArguments: args];

    NSPipe *pipe = [NSPipe pipe];
    [task setStandardOutput:pipe];

    [task launch];

    NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile];

    [task waitUntilExit];
    [task release];

Works fine so far, only thing is, that somehow, the method isn't called anymore after first reaching this point. If I don't launch the task, everything's ok, so it would seem, that the task somehow blocks further execution.

Has anybody an idea, why that would be? Any hints appreciated!

Marcus Toepper
  • 2,403
  • 4
  • 27
  • 42

1 Answers1

0

Get more control on the task by running a loop where you can read the data, check if it's timed out , etc.:

while ([task isRunning])
{
    NSData* data = [[pipe fileHandleForReading] availableData];
    if ([data length] > 0)
    {
        //process the data
    }

    // Run current runloop in default mode; check the timeout; interrupt the task if needed
}
Davyd Geyl
  • 4,578
  • 1
  • 28
  • 35