[英]NSTask to check if process is running
I'm trying to determine if a particular process if running, and tried with NSTask
but it gives me an error when I try to grep the ps
command: ps: illegal argument: |
我试图确定某个特定进程是否正在运行,并尝试使用NSTask
但是当我尝试grep ps
命令NSTask
给了我一个错误: ps: illegal argument: |
. 。
Maybe trying to use nstask for this is not the best way? 也许为此尝试使用nstask并不是最好的方法?
Here is the my code: 这是我的代码:
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);
I'm looking to return one line (if process is active) to let me know if the process is running. 我希望返回一行(如果该进程处于活动状态)以让我知道该进程是否正在运行。 This is a mac application, and it is not sandboxed. 这是一个Mac应用程序,没有沙盒。 thanks.. 谢谢..
You do not need a second task to solve this, as has been suggested, consider what you are attempting - to run a shell command line, and what runs shell command lines? 如建议的那样,您不需要第二个任务来解决此问题,请考虑您要尝试的操作-运行Shell命令行,以及运行Shell命令行的是什么?
Look up sh
in the manual and you'll see you can pass it a complete command line, including pipes, redirection, etc. as an argument. 在手册中查找sh
,您会看到可以将其作为参数传递给完整的命令行,包括管道,重定向等。 Now use NSTask
to run the shell passing the appropiriate arguments. 现在,使用NSTask
运行传递适当参数的外壳程序。
You can also use the C library system
, popen
etc. functions to do the same job. 您还可以使用C库system
, popen
等函数来完成相同的工作。
That said you might want to avoid using shell commands for this, consider for example NSWorkspace
's runningApplications
method. 也就是说,您可能想要避免使用shell命令,例如考虑NSWorkspace
的runningApplications
方法。
Addendum 附录
Now I'm not on a tablet... Here is your code modified to use the 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);
You of course will get one or two lines which match, as a line for grep
will always be there. 您当然会得到一两个匹配的行,因为grep
行将始终存在。 Why not just run ps -ax
and then use NSString
's containsString:
? 为什么不只运行ps -ax
然后使用NSString
的containsString:
? So: 所以:
task.launchPath = @"/bin/ps";
task.arguments = @[@"-ax"];
and then something like: 然后是这样的:
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 HTH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.