如何在 MacOS 中杀死进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/903076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to kill a process in MacOS?
提问by lucius
I tried kill -9 698
but the process did not die.
我试过了,kill -9 698
但过程没有死。
$ ps -ef | grep chromium
502 698 811 0 0:01.24 ?? 0:07.28 /Users/lucius/chromium/src/xcodebuild/Debug/Chromium.app/Contents/MacOS/Chromium
502 854 732 0 0:00.00 ttys001 0:00.00 grep chromium
$ kill -9 698
$ ps -ef | grep chromium
502 698 811 0 0:01.24 ?? 0:07.28 /Users/lucius/chromium/src/xcodebuild/Debug/Chromium.app/Contents/MacOS/Chromium
502 854 732 0 0:00.00 ttys001 0:00.00 grep chromium
回答by Charlie Martin
If you're trying to kill -9 it, you have the correct PID, and nothing happens, then you don't have permissions to kill the process.
如果您试图杀死 -9 它,那么您拥有正确的 PID,并且没有任何反应,那么您就没有终止该进程的权限。
Solution:
解决方案:
$ sudo kill -9 PID
Okay, sure enough Mac OS/X does give an error message for this case:
好的,Mac OS/X 确实在这种情况下给出了错误消息:
$ kill -9 196
-bash: kill: (196) - Operation not permitted
So, if you're not getting an error message, you somehow aren't getting the right PID.
因此,如果您没有收到错误消息,则说明您没有得到正确的 PID。
回答by Gajen Sunthara
Some cases you might want to kill all the process running in a specific port. For example, if I am running a node app on 3000 port and I want to kill that and start a new one; then I found this command useful.
在某些情况下,您可能希望终止在特定端口中运行的所有进程。例如,如果我在 3000 端口上运行一个节点应用程序,我想杀死它并启动一个新的;然后我发现这个命令很有用。
Find the process IDs running on TCP port 3000 and kill it
找到运行在 TCP 端口 3000 上的进程 ID 并杀死它
kill -9 `lsof -i TCP:3000 | awk '/LISTEN/{print }'`
回答by Garrett
If you know the process name you can use:
如果您知道进程名称,则可以使用:
killall Dock
If you don't you can open Activity Monitor and find it.
如果不这样做,您可以打开活动监视器并找到它。
回答by ArcaneVibe
I just now searched for this as I'm in a similar situation, and instead of kill -9 698
I tried sudo kill 428
where 428 was the pid of the process I'm trying to kill. It worked cleanly for me, in the absence of the hyphen '-' character. I hope it helps!
我刚刚搜索了这个,因为我处于类似的情况,而不是kill -9 698
我尝试sudo kill 428
428 是我试图杀死的进程的 pid。在没有连字符“-”字符的情况下,它对我来说很有效。我希望它有帮助!
回答by Alex Martelli
If kill -9
isn't working, then neither will killall
(or even killall -9
which would be more "intense"). Apparently the chromium process is stuck in a non-interruptible system call (i.e., in the kernel, not in userland) -- didn't think MacOSX had any of those left, but I guess there's always one more:-(. If that process has a controlling terminal you can probably background it and kill it while backgrounded; otherwise (or if the intense killing doesn't work even once the process is bakcgrounded) I'm out of ideas and I'm thinking you might have to reboot:-(.
如果kill -9
不起作用,那么也不会killall
(甚至killall -9
哪个会更“激烈”)。显然,chromium 进程被困在一个不可中断的系统调用中(即,在内核中,而不是在用户空间中)——认为 MacOSX 没有剩下的任何一个,但我想总会有一个:-(。如果那样的话进程有一个控制终端,您可能可以将其作为后台运行并在后台运行时将其杀死;否则(或者如果即使该进程被 bakcgrounded 强烈杀死也不起作用)我没有想法,我想您可能必须重新启动:-(。
回答by Rob Napier
Given the path to your program, I assume you're currently running this under Xcode, and are probably at a debug breakpoint. Processes cannot be killed in this state because of the underlying implementation of breakpoints.
鉴于您的程序路径,我假设您当前正在 Xcode 下运行它,并且可能处于调试断点。由于断点的底层实现,无法在此状态下终止进程。
The first step would be to go to your Xcode process and stop debugging. If for some strange reason you have lost access to Xcode (perhaps Xcode has lost access to its gdb sub-process), then the solution is to kill the gdb process. More generally, the solution here is to kill the parent process. In your case this is PID 811 (the third column).
第一步是转到您的 Xcode 进程并停止调试。如果由于某种奇怪的原因您无法访问 Xcode(可能 Xcode 无法访问其 gdb 子进程),那么解决方案是终止 gdb 进程。更一般地,这里的解决方案是杀死父进程。在您的情况下,这是 PID 811(第三列)。
There is no need to use -9 in this case.
在这种情况下不需要使用 -9。
回答by koan
I have experienced that if kill -9 PID
doesn't work and you own the process, you can use kill -s kill PID
which is kind of surprising as the man page says you can kill -signal_number PID
.
我的经验是,如果kill -9 PID
不起作用并且您拥有该过程,您可以使用kill -s kill PID
这有点令人惊讶,因为手册页说您可以kill -signal_number PID
。
回答by helloworld134
I recently faced similar issue where the atom editor will not close. Neither was responding. Kill / kill -9 / force exit from Activity Monitor - didn't work. Finally had to restart my mac to close the app.
我最近遇到了原子编辑器无法关闭的类似问题。两者都没有回应。Kill / kill -9 / force exit from Activity Monitor - 没有用。最后不得不重新启动我的 mac 以关闭该应用程序。