I have a PHP script which has mutliple sleep()
commands. I would like to execute it in my application with NSTask
. My script looks like this:
echo "first\n"; sleep(1); echo "second\n"; sleep(1); echo "third\n";
I can execute my task asynchronously using notifications:
- (void)awakeFromNib {
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/php"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-r", @"echo \"first\n\"; sleep(1); echo \"second\n\"; sleep(1); echo \"third\n\";", nil];
[task setArguments: arguments];
NSPipe *p = [NSPipe pipe];
[task setStandardOutput:p];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskExited:) name:NSTaskDidTerminateNotification object:task];
[task launch];
}
- (void)taskExited:(NSNotification *)notif {
NSTask *task = [notif object];
NSData *data = [[[task standardOutput] fileHandleForReading] readDataToEndOfFile];
NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"%@",str);
}
My output is (after 2 seconds, of course):
2011-08-03 20:45:19.474 MyApp[3737:903] first
second
third
My question is: how can I get theese three words immediately after they are printed?