[launchd-dev] An agent with Keep-Alive set to true (in this agent's plist)

Jerry Krinock jerry at ieee.org
Thu Jun 9 14:50:02 PDT 2011


On 2011 Jun 09, at 14:04, Nick wrote:

> Is there any way to solve this issue?

This may not be a "solution", but just to clarify, I think the problem comes about because the process you're keeping launched all the time is an *application*.  I use "KeepAlive" for a command-line tool and don't have this problem.

It's possible that Apple designed it this way on purpose; indeed it would be quite odd to have an *app* which the user could not quit.  And if your app is, as you say,

> (this is a kind of child protection system)

it seems that it might be better off as a background process which the child does not see.

Alternatively, you could write a little command-line tool which would do what launchd does, watching for your app to quit (this can be done by registering for NSWorkspaceDidTerminateApplicationNotification and entering a run loop) and then relaunching your app whenever it quits.  Then you would have launchd launch this command-line tool instead of your app.

I've written such a tool which watches for apps to quit.  Here is its main():

int main(int argc, const char *argv[]) {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init] ;
    
    [[NSUserDefaults standardUserDefaults] addSuiteNamed:@"com.myCompany.myApp"] ;
    
    // Create a run loop
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop] ;

    // Create a Quatcher, which will add an observer
    Quatcher* quatcher = [[Quatcher alloc] init] ;
    
    [runLoop run] ;
    
    [quatcher release] ;
    [pool release] ;
    
    return 0 ;
}

The "Quatcher" class is my "Quit Watcher".  I'll post its code if you request it.

Think about your design.  You probably don't want an app that never quits.  What you probably want is an app which sets some user defaults, loads a launchd task to "KeepAlive" a helper tool, and is then quit.  Your app's package will contain the kept-alive background helper tool which will do the work 24/7 as directed by the app's preferences.  It's a little more tricky design.  Read the message I posted yesterday in answer to a similar question on cocoa-dev:

http://lists.apple.com/archives/Cocoa-dev/2011/Jun/msg00245.html



More information about the launchd-dev mailing list