【问题标题】:Efficient way to quit Apps/process on OS X在 OS X 上退出应用程序/进程的有效方法
【发布时间】:2014-12-17 06:08:22
【问题描述】:

在 OS X 上按应用名称退出应用的最佳方式是什么?

我可以使用 Apple 脚本

NSString *scriptSource = [NSString stringWithFormat:@"tell application \"%@\" to quit",processName];
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:scriptSource];
[script executeAndReturnError:nil];

否则我可以继续使用 NSTask 执行 ps 命令,执行 grep 和 awk 来检索进程的 pid,然后使用 pid 退出进程,如下所示。

ps aux | grep -v grep |grep <process name> | awk '{print $2}'

在这种情况下,我最终使用 NSTask 4 次 + 1 次以上来终止进程。

在性能方面哪个更有效?

【问题讨论】:

  • killall 呢?
  • 发布ps aux | grep -v grep |grep &lt;process name&gt;的输出
  • ps aux | grep -v grep |grep &lt;process name&gt; | awk '{print $2}' 可以缩短为:ps aux | awk '/[p]rocess name/ {print $2}' 删除两个grep。在进程名称的第一个字母周围使用[],从输出中删除grep 搜索。

标签: objective-c xcode macos awk grep


【解决方案1】:

将输出传送到xargs 命令,如下所示。

ps aux | grep -v grep |grep <process name> | awk '{print $2}' | xargs kill

ps aux | grep -v grep | awk '/process name/{print $2}' | xargs kill

这将杀死所有 4 个进程。

【讨论】:

  • 您不需要grep -v,只需使用:ps aux | awk '/[p]rocess name/{print $2}' | xargs kill(在进程名称的第一个字母周围添加[],不包括grep本身)
猜你喜欢
  • 1970-01-01
  • 2010-11-02
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
  • 2018-11-21
  • 2018-02-17
  • 1970-01-01
相关资源
最近更新 更多