保持 NSTask 会话运行 - Cocoa

发布于 2024-11-10 03:57:52 字数 862 浏览 0 评论 0原文

我在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

廻憶裏菂餘溫 2024-11-17 03:57:52

当将 NSTask 与不会立即退出的底层程序一起使用时:

  1. 您不应该使用 -[NSFileHandle readDataToEndOfFile] 因为没有文件结尾。相反,您应该使用 -[NSFileHandle readInBackgroundAndNotify] 在后台读取该任务的标准输出管道,并在数据可用时收到通知;

  2. 仅当您确定任务不应再运行时,才应使用-[NSTask release]。在这种情况下,在释放任务之前,您应该向其标准输入发送导致底层程序退出的命令(例如相当于 control-d 的字符),或发送 -terminate-interrupt

  3. 除非您生成了辅助线程来处理该任务,否则不应使用 -waitUntilExit

When using NSTask with an underlying program that doesn’t exit immediately:

  1. 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;

  2. 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.

  3. You shouldn’t use -waitUntilExit unless you’ve spawned a secondary thread to deal with that task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文