本文介绍了快速杀死进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试制作一个Mac应用程序,该应用程序将自动关闭在OS上运行的指定代码的应用程序.我正在尝试使用killall(例如在Terminal中).每当我尝试运行程序时,都会得到"sysctl:unknown oid'killall'".
I am trying to make a Mac application that will automatically close a code designated application running on the OS. I am trying to use killall (like in Terminal). Whenever I try to run the program, I get, "sysctl: unknown oid 'killall'".
这是我的代码:
let task = Process()
task.launchPath = "/usr/sbin/sysctl"
///usr/sbin/sysctl
task.arguments = ["killall","iTunes"]
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String
print(output)
提前谢谢!
推荐答案
我建议您先阅读sysctl
的man
页-它用于获取和设置内核状态.听起来像您想要的东西吗?
I'd suggest you first read the man
page for sysctl
-- it's used to get and set kernel state. Does that sound like something you want?
killall
的路径是/usr/bin/killall
,您可以在终端机上找到
The path to killall
is /usr/bin/killall
, which you can find from Terminal:
> which killall
/usr/bin/killall
这是完整的Swift代码:
Here's the full Swift code:
let pipe = Pipe()
let task = Process()
task.launchPath = "/usr/bin/killall"
task.arguments = ["iTunes"]
task.standardOutput = pipe
task.standardError = pipe
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
if let output = String(data: data, encoding: .utf8) {
print(output)
}
这篇关于快速杀死进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!