{"id":149,"date":"2008-10-05T19:35:09","date_gmt":"2008-10-06T00:35:09","guid":{"rendered":"http:\/\/vgable.com\/blog\/2008\/10\/05\/restarting-your-cocoa-application\/"},"modified":"2009-03-18T13:33:40","modified_gmt":"2009-03-18T18:33:40","slug":"restarting-your-cocoa-application","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2008\/10\/05\/restarting-your-cocoa-application\/","title":{"rendered":"Restarting Your Mac OS X Cocoa Application"},"content":{"rendered":"<p>Restarting your own application is a tricky thing to do, because you can&#8217;t tell yourself to start up when you aren&#8217;t running.  Here is one solution, use <code><a href=\"http:\/\/vgable.com\/blog\/2008\/03\/05\/calling-the-command-line-from-cocoa\/\">NSTask<\/a><\/code> to run a very short script that you can embed right in your Objective-C code:<br \/>\n<code>kill -9 <em>YourPID<\/em><br \/>\nopen <em>PathToYourApp<\/em><\/code><br \/>\nSomething to be aware of, <code>kill -9<\/code> will immediately terminate your application, without going through the usual <code><a href=\"http:\/\/developer.apple.com\/documentation\/Cocoa\/Reference\/ApplicationKit\/Classes\/NSApplication_Class\/Reference\/Reference.html#\/\/apple_ref\/doc\/uid\/20000012-CACBFGDB\">applicationWillTerminate:<\/a><\/code> business that happens when an application quits more gracefully.<\/p>\n<p><code>- (void) restartOurselves<br \/>{<br \/>&nbsp;&nbsp;&nbsp;\/\/$N = argv[N]<br \/>&nbsp;&nbsp;&nbsp;NSString *killArg1AndOpenArg2Script = @\"kill -9 $1 \\n open \\\"$2\\\"\";<br \/>&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;\/\/NSTask needs its arguments to be strings<br \/>&nbsp;&nbsp;&nbsp;NSString *ourPID = [NSString stringWithFormat:@\"%d\",<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[NSProcessInfo processInfo] processIdentifier]];<br \/>&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;\/\/this will be the path to the .app bundle,<br \/>&nbsp;&nbsp;&nbsp;\/\/not the executable inside it; exactly what `open` wants<br \/>&nbsp;&nbsp;&nbsp;NSString * pathToUs = [[NSBundle mainBundle] bundlePath];<br \/>&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;NSArray *shArgs = [NSArray arrayWithObjects:@\"-c\", \/\/ -c tells sh to execute the next argument, passing it the remaining arguments.<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   killArg1AndOpenArg2Script,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   @\"\", \/\/$0 path to script (ignored)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   ourPID, \/\/$1 in restartScript<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   pathToUs, \/\/$2 in the restartScript<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   nil];<br \/>&nbsp;&nbsp;&nbsp;NSTask *restartTask = [NSTask launchedTaskWithLaunchPath:@\"\/bin\/sh\" arguments:shArgs];<br \/>&nbsp;&nbsp;&nbsp;[restartTask waitUntilExit]; \/\/wait for killArg1AndOpenArg2Script to finish<br \/>&nbsp;&nbsp;&nbsp;NSLog(@\"*** ERROR: %@ should have been terminated, but we are still running\", pathToUs);<br \/>&nbsp;&nbsp;&nbsp;assert(!\"We should not be running!\");<br \/>}<br \/><\/code><\/p>\n<p><strong>WARNING:<\/strong> don&#8217;t make the same mistake that I did and test <code>restartOurselves<\/code> without some kind of guard to keep your application from restarting forever.  It is <em>very<\/em> difficult to kill such a beast, because whenever it starts up it takes keyboard focus away from what you are doing&#8230;. well I&#8217;m sure you get <a href=\"http:\/\/www.youtube.com\/watch?v=LD8HDta7Z_4\">the idea<\/a>.<\/p>\n<p><code>- (BOOL) weHaveRunBefore {<br \/>\n&nbsp;&nbsp;&nbsp;NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];<br \/>\n&nbsp;&nbsp;&nbsp;BOOL weHaveRunBefore = [prefs boolForKey:@\"weHaveRunBefore\"];<br \/>\n&nbsp;&nbsp;&nbsp;[prefs setBool:YESs forKey:@\"weHaveRunBefore\"];<br \/>\n&nbsp;&nbsp;&nbsp;[prefs synchronize];<br \/>\n&nbsp;&nbsp;&nbsp;return weHaveRunBefore;<br \/>\n}<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Restarting your own application is a tricky thing to do, because you can&#8217;t tell yourself to start up when you aren&#8217;t running. Here is one solution, use NSTask to run a very short script that you can embed right in your Objective-C code: kill -9 YourPID open PathToYourApp Something to be aware of, kill -9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,6,3,5,4,9],"tags":[199,197,198],"class_list":["post-149","post","type-post","status-publish","format-standard","hentry","category-bug-bite","category-cocoa","category-macosx","category-objective-c","category-programming","category-unix","tag-bash","tag-nstask","tag-nsuserdefaults"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=149"}],"version-history":[{"count":0,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/149\/revisions"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}