NSTask Does Not Terminate NSTask Does Not Terminate unix unix

NSTask Does Not Terminate


I’ve just tested this program and it works fine: the program terminates and /tmp/apropos.txt contains the output of apropos.

#import <Foundation/Foundation.h>int main(){    NSAutoreleasePool *pool = [NSAutoreleasePool new];    NSTask *apropos = [[[NSTask alloc] init] autorelease];    NSPipe *pipe = [[[NSPipe alloc] init] autorelease];    NSFileHandle *readHandle = [pipe fileHandleForReading];    [apropos setLaunchPath:@"/usr/bin/apropos"];    [apropos setArguments:[NSArray arrayWithObjects:@"filename", @"match", nil]];    [apropos setStandardOutput:pipe];    [apropos launch];    [apropos waitUntilExit];    NSString *output = [[[NSString alloc]        initWithData:[readHandle readDataToEndOfFile]            encoding:NSUTF8StringEncoding] autorelease];    [output writeToFile:@"/tmp/apropos.txt" atomically:YES        encoding:NSUTF8StringEncoding error:NULL];    [pool drain];    return 0;}

Are you by any chance using NSLog() to inspect the output? If so, you might need to set a pipe for stdin as explained in this answer of mine to an NSTask related question. It seems that NSLog() sending data to stderr affects NSTask.


With your original code, I would imagine it's because you're not reading the output of the command. The pipes only have a limited buffer size, and if you don't read the output of the task, it can end up hung waiting for the buffer to empty out. I don't know anything about the sample code you tried so I can't help there. As for the last question, apropos only uses the pager when it's connected to a terminal. You're not emulating a terminal, so you don't have to worry. You can prove this by running apropos whatever | cat in the terminal and verifying that the pager is not invoked.