Re: Using NSTask to execute softwareupdate
Tom Jones <[email protected]> Sat, 8 Jan 2011 10:27:36 -0800
| Newsgroups | gmane.comp.macosx.devel |
|---|---|
| Message-ID | <[email protected]> |
The problem is the use of "waitUntilExit" the buffer is filling up.
Try something like this...
( You might have to clean some of it up :-) )
Tom
- (void)runTask:(NSDictionary *)patch
{
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/sbin/softwareupdate"];
[task setArguments:[NSArray arrayWithObjects:@"-i", [patch objectForKey:@"patch_identifier"], nil]];
[task setEnvironment:[NSDictionary dictionaryWithObject:@"1" forKey:@"COMMAND_LINE_INSTALL"]];
NSPipe *install_pipe = [NSPipe pipe];
[task setStandardOutput: install_pipe];
[task setStandardError: install_pipe];
NSFileHandle *file = [install_pipe fileHandleForReading];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(taskEnded:)
name: NSTaskDidTerminateNotification
object: file];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(readFromTask:)
name: NSFileHandleDataAvailableNotification
object: file];
[file readInBackgroundAndNotify];
[task launch];
[task release];
}
- (void)readFromTask:(NSNotification *)aNotification
{
NSData *data = [[aNotification userInfo] objectForKey: NSFileHandleNotificationDataItem];
if ([data length]) {
/* Process data */
[[aNotification object] readInBackgroundAndNotify];
}
}
- (void)taskEnded:(NSNotification *)aNotification
{
// Task is full complete
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
On Jan 8, 2011, at 9:59 AM, Colin Swelin wrote:
> Here's the basic NSTask stuff:
>
>
> NSPipe *pipes = [NSPipe pipe];
> NSTask* patchInstall = [[NSTask alloc] init];
> [patchInstall setLaunchPath:@"/usr/sbin/softwareupdate"];
> [patchInstall setArguments:[NSArray arrayWithObjects:@"-i", [patch objectForKey:@"patch_identifier"], nil]];
>
> [patchInstall setStandardOutput: pipes];
>
> NSFileHandle *file = [pipes fileHandleForReading];
>
> [patchInstall launch];
>
> NSData *data = [file readDataToEndOfFile];
>
> [patchInstall waitUntilExit];
>
>
> ......
> [patchInstall release];
>
>
> So i'm waiting for the task to complete, then I do my thing with the results.
>
> Thanks,
> Colin
>
> On Sat, Jan 8, 2011 at 12:48 PM, Tom Jones <[email protected]> wrote:
> Hi Colin,
> I would need to see your NSTask code, to really know. Are you using notifications as data becomes available or are you waiting until the task has completed?
>
>
> Thanks,
> tom
>
>
> On Jan 6, 2011, at 8:54 AM, Colin Swelin wrote:
>
> > Hi,
> >
> > I"m currently using NSTask to execute softwareupdate to install patches, however, there's a few patches that seem to hang when trying to install. For example, the latest 10.6.6 update and a older iTunes patch, everything else seems to work perfectly fine.
> >
> > Anyone have any clues to why this might happen? I've set the "COMMAND_LINE_INSTALL" environment variable to 1, so i'm a little confused to why this is happening.
> >
> > Thanks._______________________________________________
> > MacOSX-dev mailing list
> > [email protected]
> > http://www.omnigroup.com/mailman/listinfo/macosx-dev
>
>