• 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: Submit background task
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Submit background task


  • Subject: Re: Submit background task
  • From: John Nairn <email@hidden>
  • Date: Fri, 17 Jun 2005 11:25:41 +0200

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--

Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org



--------------- John Nairn (1-801-581-3413, FAX:1-801-581-4816) Web Page: http://www.mse.utah.edu/~nairn

_______________________________________________
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: 
 >Submit background task (From: John Nairn <email@hidden>)
 >Re: Submit background task (From: Sherm Pendley <email@hidden>)

  • Prev by Date: Performance issue : NSDictionary against NSXMLNode and NSXMLParser
  • Next by Date: NSCombobox Problem - how to control width of pop up list
  • Previous by thread: Re: Submit background task
  • Next by thread: NSFormFeedCharacter does not trigger container break
  • Index(es):
    • Date
    • Thread