如何按进程名称检查 mac 进程是否存在
How to check a mac process exists by process name
>我需要杀死一个 mac 进程,但在此之前我需要检查它是否真的存在?
我尝试使用C++方法system("killall process_name");
按名称杀死进程。
但我想我还应该检查进程是否实际运行。 谁能告诉我,如何做到这一点?
BOOL processIsRunning = system("ps -Ac | grep 'AProcessName' > /dev/null") == 0;
或者,如果您特别要检查字符串:
BOOL processIsRunning = system("ps -ef | grep 'AUniqueString' | grep -iv grep > /dev/null") == 0;
您可以安全地使用killall - 您已经在使用的那个。它是无害的,即使该过程不存在。
既然你已经标记了Objective-C,我假设你对可可解决方案很好。如果您手头有进程名称,这是另一种杀死进程的简单方法。它使用苹果脚本。在这里,您无需检查它是否正在运行。
NSString *processName = @"Microsoft Outlook";
NSString *scriptSource = [NSString stringWithFormat:@"tell application "%@" to quit",processName];
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:scriptSource];
[script executeAndReturnError:nil];
相关文章:
- 由 JOB 中的进程启动的子进程是否可以将 JOB 属性设置为脱离作业?
- 通过 pid 窗口判断进程是否存在
- 多个线程/进程是否可以在不同步的情况下同时从/写入文件的非重叠区域?
- 如何检查进程是否在C++中运行?
- 如何判断从 CreateProcess 启动的进程是否仍在运行
- (LINX-IPC 协议)如何确定具有 PID 的进程是否在C++中运行
- 等待() 等待进程是否消耗系统 CPU
- 无法检测子进程是否已终止
- Linux - 查找进程是否已准备就绪或正在运行
- 检查给定进程是否正在运行
- 检查特定进程是否正在运行的快速方法
- SYSTEM进程是否可以与非SYSTEM进程共享数据
- 是否可以使用带有错误代码的OpenProcess ERROR_ACCESS_DENIED来了解进程是否存在
- 如何检查进程是否具有图形界面(没有 .NET)
- 检查进程是否存在
- 如何按进程名称检查 mac 进程是否存在
- Boost.进程检查进程是否终止
- 检测 Linux 下的 32 位进程是否在 64 位环境中运行
- 如何检测进程是否正在调试
- 检查相邻从进程是否在MPI中结束