Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSTask检查进程是否正在运行_Objective C_Macos_Cocoa_Nstask - Fatal编程技术网

Objective c NSTask检查进程是否正在运行

Objective c NSTask检查进程是否正在运行,objective-c,macos,cocoa,nstask,Objective C,Macos,Cocoa,Nstask,我正在尝试确定某个特定进程是否正在运行,并尝试使用NSTask,但在尝试grepps命令时出现错误:ps:非法参数:| 可能尝试使用nstask进行此操作不是最好的方法? 以下是我的代码: NSString *process = @"grep \"/usr/sbin/notifyd\""; NSTask *task; task = [[NSTask alloc] init]; [task setLaunchPath:@"/bin/ps"]; NSArray *arguments; argum

我正在尝试确定某个特定进程是否正在运行,并尝试使用
NSTask
,但在尝试grep
ps
命令时出现错误:
ps:非法参数:|

可能尝试使用nstask进行此操作不是最好的方法?

以下是我的代码:

NSString *process = @"grep \"/usr/sbin/notifyd\"";

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/ps"];

NSArray *arguments;
arguments = [NSArray arrayWithObjects:@"ax",process,nil]; // works, but returns all processes
// arguments = [NSArray arrayWithObjects:@"ax", @"|", process,nil]; // returns illegal argument

[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 (@"result:\n%@", string);

我希望返回一行(如果进程处于活动状态),让我知道进程是否正在运行。这是一个mac应用程序,它不是沙盒。谢谢…

< p>你不需要第二个任务来解决这个问题,如所建议的,考虑你正在尝试的——运行shell命令行,以及运行shell命令行?

在手册中查找
sh
,您将看到可以向其传递一个完整的命令行,包括管道、重定向等作为参数。现在使用
NSTask
运行shell,传递相应的参数

您还可以使用C库
系统
popen
等函数来完成相同的工作


您可能希望避免使用shell命令,例如,考虑“代码> NSWorkStudio代码>运行应用程序< /Cult>方法> < /P> 附录

现在我不在平板电脑上。。。以下是修改后使用shell的代码:

NSTask *task = [NSTask new];

task.launchPath = @"/bin/sh";
task.arguments = @[@"-c", @"ps -ax | grep /usr/bin/notifyd"];

NSPipe *pipe = [NSPipe pipe];
task.standardOutput = pipe;

NSFileHandle *file = [pipe fileHandleForReading];

[task launch];

NSData *data = [file readDataToEndOfFile];
NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@"result:\n%@", string);
当然,您会得到一行或两行匹配的代码,因为
grep
的代码行将始终存在。为什么不运行
ps-ax
,然后使用
NSString
containsString:
?因此:

task.launchPath = @"/bin/ps";
task.arguments = @[@"-ax"];
然后是这样的:

NSData *data = [file readDataToEndOfFile];
NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
BOOL notifydIsRunning = [string containsString:@"/usr/sbin/notifyd"];
NSLog (@"notifydIsRunning: %@", notifydIsRunning ? @"yes" : @"no");

HTH

您不能像在Terminal.app中那样通过管道传输多个命令。您需要第二个
NSTask
,并将第一个输入的结果传递给标准输入。@vadian:我是这么想的,但不确定。但第二个NSTask的问题是它永远不会完成。在尝试grep第一个任务的结果时,它似乎是挂起的。nstask是最好的方法吗?有C函数(在
sysctl
中)可以获取进程名称的pid,该函数还可以检查此特定进程是否正在运行。@vadian:我发现了另一种方法,我相信使用
kinfo\u proc
,谢谢。
runningApplications
除非是一个应用程序包,否则无法正常工作。我认为它不会正常工作?@NikoFederov-抱歉,我没有注意到您正在搜索
notifyd
,因此
运行应用程序
不是一个选项。我添加了一个附录,其中的代码显示了如何使用shell,或者如何避免它。。。