Restarting App

Discussion in 'Mac Programming' started by davbeck, Oct 28, 2009.

  1. davbeck macrumors regular

    May 16, 2008
    Reno, NV
    What is the best way to restart my Cocoa app? I saw one example online but it uses kill which will force quite the app.
  2. kainjow Moderator emeritus


    Jun 15, 2000
    You could write a daemon process to handle this if you don't want to use kill. The daemon could launch with args for the process name/pid to quit. You could then use Apple Events (e.g. "tell application 'App' to quit") to handle this smoothly at first and wait for a termination notification (e.g. NSWorkspaceDidTerminateApplicationNotification). If that's not received within a given amount of time you could then kill it like normal. 10.6 adds a NSRunningApplication class which would make this entire process easier.
  3. chown33 macrumors 604

    Aug 9, 2009
    on the Western Slopes, with E. A. Poe
    In your app, create a pipe. Start a child process. Have it read the pipe from its parent, your app. When the pipe gets an EOF, it should relaunch your app.

    In your app, after the child process is launched, do your app's normal cleanup and termination, but leave the sending end of the pipe open.

    When your app exits, the system will automatically close the sending end of the pipe, which your lingering child then detects as an EOF on its reading end, which triggers its action of relaunching your app.

    That's the gist of it. You may have to arrange for some signals to be ignored in the child. You may also want a timeout in the child, to abort if an EOF doesn't appear within X seconds (BSD alarm() function, SIGALRM signal). Experiment.

    The source for the Sparkle framework has code for a shell script that does approximately what I outlined above, although it polls rather than reading a pipe. Or at least it used to. It may be different now.

Share This Page