Oct 28, 2009, 08:40 PM
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.
Oct 28, 2009, 11:53 PM
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.
Oct 29, 2009, 01:39 AM
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.