保持 NSTask 会话运行 - Cocoa
我在我的 Cocoa 应用程序中运行一个简单的 grep 命令,如下所示:
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/grep"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"foo", @"bar.txt", nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@"grep returned:\n%@", string);
[string release];
[task release];
但是,我有点好奇如何通过终端输入命令,这些命令不会给出输出,并且不会立即执行,除非使用 Control< 之类的命令退出/kbd> + C 可以使用此技术运行。比如运行 java -jar server.jar ,它会一直运行直到退出会话。一旦命令启动,会话不会自动结束,我该如何做类似的事情?
我是否只需要注释掉它释放 NSTask 的部分?任何建议都会很好!
I run a simple grep command in my Cocoa app like so:
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/grep"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"foo", @"bar.txt", nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@"grep returned:\n%@", string);
[string release];
[task release];
However, I am somewhat curious to know how commands entered through terminal, which don't give out an output and are not executed promptly unless exited with something like Control + C can be run with this technique. Something like running java -jar server.jar
where it keeps running until quit out of the session. How would I do something like that where the session is not automatically ended once the command has been launched?
Would I just need to comment out the part where it releases the NSTask
? Any suggestions would be nice!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当将
NSTask
与不会立即退出的底层程序一起使用时:您不应该使用
-[NSFileHandle readDataToEndOfFile]
因为没有文件结尾。相反,您应该使用-[NSFileHandle readInBackgroundAndNotify]
在后台读取该任务的标准输出管道,并在数据可用时收到通知;仅当您确定任务不应再运行时,才应使用
-[NSTask release]
。在这种情况下,在释放任务之前,您应该向其标准输入发送导致底层程序退出的命令(例如相当于 control-d 的字符),或发送-terminate
或-interrupt
。除非您生成了辅助线程来处理该任务,否则不应使用
-waitUntilExit
。When using
NSTask
with an underlying program that doesn’t exit immediately:You shouldn’t use
-[NSFileHandle readDataToEndOfFile]
since there is no end of file. Instead, you should use-[NSFileHandle readInBackgroundAndNotify]
to read the standard output pipe of that task in the background, and be notified when data is available;You should use
-[NSTask release]
only when you’ve determined that the task shouldn’t run any more. In that case, prior to releasing the task, you should send its standard input the command that causes the underlying program to exit (e.g. the characters equivalent to control-d), or send it-terminate
or-interrupt
.You shouldn’t use
-waitUntilExit
unless you’ve spawned a secondary thread to deal with that task.