0

I am trying to relaunch my application with some command line arguments. The below code works (without command line arguments)

- (void)relaunchAfterDelay:(float)seconds
{
NSLog(@"relaunchAfterDelay ") ;
NSTask *task = [[NSTask alloc] init] ;
NSArray * args = [ NSArray arrayWithObjects:@"-c", [NSString stringWithFormat:@"sleep %f; open \"%@\"", seconds, [[NSBundle mainBundle] bundlePath]], nil ] ;
[task setLaunchPath:@"/bin/sh"];
[task setArguments:args];
[task launch];
[NSNotificationCenter defaultCenter ] postNotificationName : @"Terminate" object : self ] ;
}

But if I add the required command line arguments to the args array, the app terminates but doesn't relaunch.

I did something like this :

NSMutableArray *obj = [ NSMutableArray arrayWithObjects:@"-c",  @"updated", nil ] ;
NSArray * args = [ NSArray arrayWithObjects:obj, [NSString stringWithFormat:@"sleep %f; open \"%@\"", seconds, [[NSBundle mainBundle] bundlePath]], nil ] ;

"updated" being the required argument to be passed to the new instance. What am I missing?

Any help is appreciated.

Antony Raphel
  • 2,036
  • 2
  • 25
  • 45
Anand Srinivasan
  • 450
  • 5
  • 20
  • What should the args for sh be and what is `args`? – Willeke May 25 '17 at 11:58
  • @Anand do you want to relaunch your cocoa app by command line tool ? so why don't you add a shell script in you project so that you only have to terminate your app and your shell tool will launch your app.. – Vikram Sinha May 25 '17 at 12:37
  • @Willeke the args for sh should be some string. I'm trying to relaunch the application after update. So I will pass some arguments as string. And `args` is the arguments array for `NSTask *task` – Anand Srinivasan May 26 '17 at 05:50
  • Did you check the command in the Terminal? Did you check if `args` is what is should be? – Willeke May 26 '17 at 09:10

0 Answers0