我有一个应用程序检查其命令行参数并将值存储在持久存储中。其中一个是密码,我不想让人们看到'ps'和朋友。我目前正在研究的方法是,在我存储了我需要的值之后,在没有命令行参数的情况下重新启动进程。我天真的方法就是这样,其中args [0]是应用程序的路径:
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:[args objectAtIndex:0]];
[task launch];
[task release];
[NSApp terminate:nil];
孩子跑了。然而,当我的应用程序被终止时,孩子似乎不是孤儿,而是被卡住了。我在这个问题上离开了吗?
更多信息:所以看来当我调用[NSApp terminate:nil]时,启动的NSTask卡住了,但是如果我只是退出()那么它工作正常。但是,如果我这样做,我担心开放的东西(钥匙串,plist等)会处于不良状态。
请注意,许多示例代码都是关于某些类似监视程序的进程,它会在需要时重新启动单独的进程。我正在尝试重新启动已在同一进程内运行的当前进程。
答案 0 :(得分:10)
网上有很多例子,但是this one(也在下面)看起来它有你需要的所有代码。那里也有more detailed explanations。
// gcc -Wall -arch i386 -arch ppc -mmacosx-version-min=10.4 -Os -framework AppKit -o relaunch relaunch.m
#import <AppKit/AppKit.h>
@interface TerminationListener : NSObject
{
const char *executablePath;
pid_t parentProcessId;
}
- (void) relaunch;
@end
@implementation TerminationListener
- (id) initWithExecutablePath:(const char *)execPath parentProcessId:(pid_t)ppid
{
self = [super init];
if (self != nil) {
executablePath = execPath;
parentProcessId = ppid;
// This adds the input source required by the run loop
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationDidTerminate:) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
if (getppid() == 1) {
// ppid is launchd (1) => parent terminated already
[self relaunch];
}
}
return self;
}
- (void) applicationDidTerminate:(NSNotification *)notification
{
if (parentProcessId == [[[notification userInfo] valueForKey:@"NSApplicationProcessIdentifier"] intValue]) {
// parent just terminated
[self relaunch];
}
}
- (void) relaunch
{
[[NSWorkspace sharedWorkspace] launchApplication:[NSString stringWithUTF8String:executablePath]];
exit(0);
}
@end
int main (int argc, const char * argv[])
{
if (argc != 3) return EXIT_FAILURE;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[[TerminationListener alloc] initWithExecutablePath:argv[1] parentProcessId:atoi(argv[2])] autorelease];
[[NSApplication sharedApplication] run];
[pool release];
return EXIT_SUCCESS;
}
答案 1 :(得分:2)
创建一个外部进程,在其终止时启动您的进程。然后终止。使用NSTask启动Cocoa程序并不是很正常。
答案 2 :(得分:2)
我知道回答有点迟,但这个答案可能对其他人有所帮助。这是一个可以帮助你的很酷的技巧。
使用terminal命令,只需将您的应用程序作为新实例打开,终止当前实例。
这是如何完成的:
....
NSString* path = [[NSBundle mainBundle] bundlePath];
NSString* cmd = [NSString stringWithFormat:@"open -n %@", path];
[self runCommand:cmd];
exit(0);
}
/// temrinal function
-(NSString*)runCommand:(NSString*)commandToRun;
{
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/sh"];
NSArray *arguments = [NSArray arrayWithObjects:
@"-c" ,
[NSString stringWithFormat:@"%@", commandToRun],
nil];
NSLog(@"run command: %@",commandToRun);
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *output;
output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
return output;
}
答案 3 :(得分:0)
对于仍然想要使用NSTask重新启动的人,我找到了一种可能的方法:请不要设置NSTask的NSPipe,因为NSTask将终止应用程序本身,一旦应用程序终止,启动的NSTask可能会卡住那里。
至少在我删除NSPipe设置后,我的应用程序重新启动成功。 以下是我的工作: 1.编写一个命令行工具,它有3个参数:app bundle id,app full path,请注意在这个命令行中你需要终止应用程序并等待一段时间以确保它在启动新的之前真正终止,我一直在检查app.isTerminated并睡觉(1)如果它没有被终止。