简体   繁体   English

如何在 MacOS 中杀死进程?

[英]How to kill a process in MacOS?

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

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。

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;例如,如果我在 3000 端口上运行一个节点应用程序,我想杀死它并启动一个新的; then I found this command useful.然后我发现这个命令很有用。

Find the process IDs running on TCP port 3000 and kill it找到运行在 TCP 端口 3000 上的进程 ID 并杀死它

kill -9 `lsof -i TCP:3000 | awk '/LISTEN/{print $2}'`

If you know the process name you can use:如果您知道进程名称,则可以使用:

killall Dock

If you don't you can open Activity Monitor and find it.如果不这样做,您可以打开活动监视器并找到它。

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.我刚刚搜索了这个,因为我处于类似的情况,而不是kill -9 698我尝试了sudo kill 428其中 428 是我试图sudo kill 428的进程的pid。 It worked cleanly for me, in the absence of the hyphen '-' character.在没有连字符“-”字符的情况下,它对我来说很有效。 I hope it helps!我希望它有帮助!

If kill -9 isn't working, then neither will killall (or even killall -9 which would be more "intense").如果kill -9不起作用,那么killall也不会起作用(甚至killall -9会更“激烈”)。 Apparently the chromium process is stuck in a non-interruptible system call (ie, 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:-(.显然,chromium 进程被困在一个不可中断的系统调用中(即,在内核中,而不是在用户空间中)——认为 MacOSX 没有留下任何一个,但我想总会有一个:-(。如果那样的话进程有一个控制终端,您可能可以将其作为后台运行并在后台运行时将其杀死;否则(或者如果即使该进程被 bakcgrounded 强烈杀死也不起作用)我没有想法,我想您可能必须重新启动:-(。

Given the path to your program, I assume you're currently running this under Xcode, and are probably at a debug breakpoint.鉴于您的程序的路径,我假设您当前正在 Xcode 下运行它,并且可能处于调试断点。 Processes cannot be killed in this state because of the underlying implementation of breakpoints.由于断点的底层实现,无法在此状态下终止进程。

The first step would be to go to your Xcode process and stop debugging.第一步是转到您的 Xcode 进程并停止调试。 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.如果由于某些奇怪的原因您无法访问 Xcode(可能 Xcode 无法访问其 gdb 子进程),那么解决方案是终止 gdb 进程。 More generally, the solution here is to kill the parent process.更一般地说,这里的解决方案是杀死父进程。 In your case this is PID 811 (the third column).在您的情况下,这是 PID 811(第三列)。

There is no need to use -9 in this case.在这种情况下不需要使用 -9。

我的经验是,如果kill -9 PID不起作用并且您拥有该进程,则可以使用kill -s kill PID这有点令人惊讶,因为手册页说您可以kill -signal_number PID

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. Kill / kill -9 / 强制退出活动监视器 - 没有用。 Finally had to restart my mac to close the app.最后不得不重新启动我的 mac 以关闭该应用程序。

in the spotlight, search for Activity Monitor.在聚光灯下,搜索活动监视器。 You can force fully remove any application from here.您可以从此处强制完全删除任何应用程序。

在此处输入图片说明

Do you can list the process using a port with command lsof , for example:您是否可以使用带有命令lsof的端口列出进程,例如:

lsof -i tcp:PORT_NUMBER_HERE

Replace the word PORT_NUMBER_HERE to the port number that you are using, then a the process running on the port will be listed.将单词PORT_NUMBER_HERE替换为您正在使用的端口号,然后将列出在该端口上运行的进程。 Then you have just to kill the process ID founded like this:然后你只需要杀死这样创建的进程 ID:

kill PID_NUMBER

Where PID_NUMBER is the process ID running on the port.其中PID_NUMBER是在端口上运行的进程 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM