I think the problem is solved. I had submitted the task asynchronously using /bin/csh and one-line command to submit the background job. I submitted it using a TaskWrapper which I wrote based on the Moriarity task wrapper. One thing I found I had to add was to check the stdout pipe after the task was terminated because some tasks with long output could lose the last few lines of output without this check. Thus after the task finishes (or is forces to finish), I had a loop to capture all remaining data:
while((data="" availableData]) && [data length]) { [controller appendOutput:[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]]; }
This never caused a problem before, but when the task submitted a background job, the loop would always get stuck in the first call for availableData and wait there until the background task was done too. The problem was solved by skipping this final check when submitted a task for background.
Next I would like to submit the background task to run on a different computer, but that can wait until I buy more computers. On Jun 17, 2005, at 2:05 PM, Ben Dougall wrote: there's two ways of launching an NSTask. synchronously and asynchronously. an example of using an asynchronous NSTask can be found here < http://www.cocoaprogramming.net/Downloads.html> in the Animal example in the code that can be downloaded from there. AnimalController.m > -(void)_startAnimalProcess is the method to look at.
hth, ben.
On Friday, June 17, 2005, at 10:25 am, John Nairn wrote:
It partly works. I used NSTask to run /bin/csh with arguments "-c" and my command to submit a background job such as
"/path/to/code -args > /output/file &
The NSTasks succeeds in submitting a background job and output of NSTask gives me the pid for the newly submitted job. The problem is, however, that NSTask then sits there and waits until the background job is finished. If I kill the submitted job manually, the NSTask is happy and exits. If I kill my main application, the submitted job keeps running (like I want). The only problem, therefore, is I need the NSTask to terminate once the job is submitted and return control to my application rather than wait until the background task is finished.
On Jun 17, 2005, at 2:25 AM, Sherm Pendley wrote:
On Jun 16, 2005, at 10:24 AM, John Nairn wrote:
What is the best way (NSTask or other) to submit a background process that will continue running even if the submitting application quits? I tried an NSTask adding arguments ">", "output file name", and "&", to redirect to a file and run in background, but NSTask just passed the new arguments to the main task.
NSTask simply passes arguments as given to the launched task - it doesn't interpret them like a command shell would. If you want to do that, you could use NSTask to run /bin/sh, and pass the shell command you want to execute as an argument, using sh's -c option. Something like this:
NSArray *args = [NSArray arrayWithObjects:
// -c & shell command first @"-c", @"/path/to/script > /output/file 1>&2 &",
// Additional arguments are passed to the script @"--foo", @"--bar",
// Terminate the list nil];
[NSTask launchedTaskWithLaunchPath:@"/usr/bin/sh" arguments:args];
Especially handy if you want to use multiple pipes in series, something that's easy to do with | in a shell, but would require multiple instances of NSPipe to do in code.
Another option would be to use old-school Unix functions fork() and exec().
sherm--
--------------- John Nairn (1-801-581-3413, FAX:1-801-581-4816) |