• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSTask keeps running - how to stop?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTask keeps running - how to stop?


  • Subject: Re: NSTask keeps running - how to stop?
  • From: Robert Cerny <email@hidden>
  • Date: Tue, 15 Mar 2005 09:43:34 +0100

- (void)terminate

Sends a terminate signal to the receiver and all of its subtasks. If the task terminates as a result, which is the default behavior, an NSTaskDidTerminateNotification gets sent to the default notification center. This method has no effect if the receiver was already launched and has already finished executing. If the receiver has not been launched yet, this method raises an NSInvalidArgumentException.
It is not always possible to terminate the receiver because it might be ignoring the terminate signal. terminate sends SIGTERM.


See Also: + launchedTaskWithLaunchPath:arguments:, – launch, – terminationStatus, – waitUntilExit

HTH
Robert

On 15.3.2005, at 9:03, Stephan Ruggiero wrote:

Hello list,

I am quite new to cocoa development and currently writing a little tool to control some shell scripts from a GUI. Everything works just fine, but I encountered a problem I do not find a solution to:

I want the user to be able to monitor six different log files (only one at a time), so I placed six TextViews in a TabView. Each time a tab is selected, I call an NSTask to start "tail -f -n 100" with the appropriate file name. It should provied "Console.app"-like functionality.
To avoid having the GUI blocked I start a new thread to listen to the tail task and write new data to the appropriate TextView.
This is an extract of the code I use:


-(void)loadLogData:(NSString *)logToDisplay { //logToDisplay is given depending on the current TabViewItem's identifier
NSString *pathToLog = [....] // I give the path here
NSTask *tail=[[NSTask alloc] init];
NSPipe *pipe=[[NSPipe alloc] init];
NSFileHandle *handle;
int linesToDisplay=100;

[tail setLaunchPath:@"/usr/bin/tail"];
[tail setArguments:[NSArray arrayWithObjects:@"-f",[@"-n"stringByAppendingString:[NSString stringWithFormat:@" %i",linesToDisplay]],pathToLog,nil]];
[tail setStandardOutput:pipe];
handle=[pipe fileHandleForReading];
[tail launch];

[NSThread detachNewThreadSelector:@selector(copyData:) toTarget:self withObject:handle];

[pipe release];
[tail release];
}


- (void)copyData:(NSFileHandle *)handle
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
NSData *data;
while ([data=[handle availableData] length]) {
NSString *logTxt=[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSRange theEnd=NSMakeRange([[txtView string] length],0);
[txtView replaceCharactersInRange:theEnd withString:logTxt];
theEnd.location+=[logTxt length];
[txtView scrollRangeToVisible:theEnd];
[logTxt release];
}
[pool release];
}


This works fine, but my problem is that I do not know how to terminate the NSTask (tail) - if the user switches to an other tab, a second "tail" is started, then a third one... I had 20 "tails" running around here :-)

For now I implemented a BOOL variable to know if a "tail" was already started and if so I call a shell script that determines the PID and kills the process - quite a dirty solution and it causes the GUI to react slower.

Do you know some other way to terminate the "tail -f" NSTask from cocoa?
(I tried to call [tail terminate] right before [pipe release] to see if tail is stopped, but it keeps running...)


Thank you very much for your help!

Stephan  _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >NSTask keeps running - how to stop? (From: Stephan Ruggiero <email@hidden>)

  • Prev by Date: NSTask keeps running - how to stop?
  • Next by Date: newbie confused by obj-c syntax
  • Previous by thread: NSTask keeps running - how to stop?
  • Next by thread: newbie confused by obj-c syntax
  • Index(es):
    • Date
    • Thread