I'm writing an application that kicks off a subprocess running a simple web server. I am using NSTask and communicating with it with pipes, and everything seems more or less fine. However, if my program crashes, the subprocess is left alive and the next time I launch the app there is a conflict between the old subprocess and the new one. Is there any way to ensure that subprocesses die when the owning app dies?
We started with Q&A. Technical documentation is next, and we need your help.
Whether you're a beginner or an experienced developer, you can contribute.
|
|
None of the above works… Not even
This serves a dual purpose.. it enables you to "keep an eye on the kids..", and in the sad event of parentcide (or horrible car accident) - kill-off the zombie orphans. Then, you pull the trigger on yourself (you being the shell script) and your process table will be clean.. as if you never existed. No blocked ports, no conflicts on relaunch, no app-store rejections. Lemme know if this helps! Update: I made a Xcode template / daemon / project / whatever that does the trick. Check it out.. mralexgray / Infanticide. |
||||
|
|
|
Your application delegate can implement the
message, and terminate the NSTask there. However, it is not guaranteed that during a crash, this delegate will be called. Two additional steps you can take:
|
|||
|
|
|
Following code sample should help you. it is borrowed from here,
|
|||
|
|
|
UPDATE: Now that I go to check this properly it doesn't work. Trying to set the process group fails with this error; EPERM "The effective user ID of the requested process is different from that of the caller and the process is not a descendant of the calling process." There is a more recent thread on this issue but no easy solution as far as I can tell http://www.omnigroup.com/mailman/archive/macosx-dev/2009-March/062164.html I've tried a suggestion by Robert Pointon on Cocoadev in my app. I haven't got around to testing it yet though. http://www.cocoadev.com/index.pl?NSTaskTermination The idea is to set the process group of the task to be the same as that of the process that launches the task (note: the code below is basically lifted from the thread above).
|
||||
|
|