postgresql 如何杀死不会死的postgres进程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25086725/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:33:56  来源:igfitidea点击:

How to kill postgres processes that won't die?

postgresql

提问by Learning stats by example

I have the following processes running per ps aux | grep postgres

我有以下进程运行 ps aux | grep postgres

postgres 8720 0.0 0.0 2492848 5652 ?? SN 2:33PM
0:00.04 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker -s mdworker -c MDSImporterWorker -m com.apple.mdworker.shared postgres 495 0.0 0.0 2514428 1776 ?? S 1:57PM 0:00.07 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdflagwriter postgres 491 0.0 1.0 2669764 166180 ?? Ss 1:57PM
0:10.31 com.apple.IconServicesAgent postgres 490 0.0 0.0 2505832 2884 ?? Ss 1:57PM 0:00.07 /usr/libexec/xpcd postgres 485 0.0 0.0 2514284 1284 ?? S 1:57PM 0:00.07 /usr/sbin/cfprefsd agent postgres 484 0.0 0.0 2536788
1708 ?? S 1:57PM 0:00.06 /usr/sbin/distnoted agent postgres
479 0.0 0.0 2508256 1100 ?? Ss 1:57PM 0:00.06 /sbin/launchd postgres 427 0.0 0.0 2493792 608 ?? Ss 1:55PM 0:00.02 postgres: stats collector process postgres 426 0.0 0.0 2654624 2208 ?? Ss 1:55PM 0:00.02 postgres: autovacuum launcher process postgres 425 0.0 0.0 2646300 764 ?? Ss 1:55PM 0:00.03 postgres: wal writer process postgres 424 0.0 0.0 2638108 1608 ?? Ss 1:55PM 0:00.07 postgres: writer process postgres 423
0.0 0.0 2638108 836 ?? Ss 1:55PM 0:00.00 postgres: checkpointer process postgres 419 0.0 0.0 2493792
500 ?? Ss 1:55PM 0:00.00 postgres: logger process postgres 85 0.0 0.1 2638108 13844 ?? Ss 1:55PM 0:00.04 /Library/PostgreSQL/9.3/bin/postmaster -D/Library/PostgreSQL/9.3/data Username 8910 0.0 0.0 2432784 612 s003 R+ 2:35PM
0:00.00 grep postgres

postgres 8720 0.0 0.0 2492848 5652 ?? SN 2:33PM
0:00.04 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker -s mdworker -c MDSImporterWorker -m com.apple.mdworker.shared postgres 495 0.0 0.00. 2514428 1776 ?? S 1:57PM 0:00.07 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdflagwriter postgres 491 0.0 1.0 2669764 166180 ?? Ss 1:57PM
0:10.31 com.apple.IconServicesAgent postgres 490 0.0 0.0 2505832 2884 ?? Ss 1:57PM 0:00.07 /usr/libexec/xpcd postgres 485 0.0 0.0 2514284 1284 ?? S 1:57PM 0:00.07 /usr/sbin/cfprefsd 代理 postgres 484 0.0 0.0 2536788
1708 ?? S 1:57PM 0:00.06 /usr/sbin/distnoted 代理 postgres
479 0.0 0.0 2508256 1100 ?? Ss 1:57PM 0:00.06 /sbin/launchd postgres 427 0.0 0.0 2493792 608 ?? Ss 1:55PM 0:00.02 postgres:统计信息收集器进程 postgres 426 0.0 0.0 2654624 2208 ?? Ss 1:55PM 0:00.02 postgres:autovacuum 启动器进程 postgres 425 0.0 0.0 2646300 764 ?? Ss 1:55PM 0:00.03 postgres:wal writer 进程 postgres 424 0.0 0.0 2638108 1608 ?? Ss 1:55PM 0:00.07 postgres:写入进程 postgres 423
0.0 0.0 2638108 836 ?? Ss 1:55PM 0:00.00 postgres:检查点进程 postgres 419 0.0 0.0 2493792
500 ?? Ss 1:55PM 0:00.00 postgres:记录器进程 postgres 85 0.0 0.1 2638108 13844 ?? Ss 1:55PM 0:00.04 /Library/PostgreSQL/9.3/bin/postmaster -D/Library/PostgreSQL/9.3/data 用户名 8910 0.0 0.0 2432784 612 s003 R+ 2:35PM
0:00.00 grep postgres

I want them dead!

我要他们死!

But killall postgres gives me:

但是 killall postgres 给了我:

No matching processes belonging to you were found

I understand I need to use:

我知道我需要使用:

kill -INT `head -1 /usr/local/pgsql/data/postmaster.pid`

But I do not understand this command.

但我不明白这个命令。

Guidance is appreciated. Long frustrating morning!

指导表示赞赏。漫长的令人沮丧的早晨!

回答by CmdrMoozy

In general, you can use the killcommand to stop running processes. More specifically, killsends signals to processes. The most basic kill command is something like this:

通常,您可以使用该kill命令停止正在运行的进程。更具体地说,kill向进程发送信号。最基本的kill命令是这样的:

kill <pid>

kill <pid>

This sends the SIGTERMsignal to the process, telling them to stop.

这会向SIGTERM进程发送信号,告诉他们停止。

Occasionally, you'll have a process that is really stuck, and doesn't seem to respond to SIGTERM. In this case, you can tell the killcommand to do it a little (or, you know, a lot) more forcefully by doing:

有时,您会遇到一个真正卡住的进程,并且似乎没有响应 SIGTERM。在这种情况下,您可以通过kill执行以下操作来告诉命令更强有力地执行此操作:

kill -9 <pid>or, equivalently, kill -KILL <pid>

kill -9 <pid>或者,等效地, kill -KILL <pid>

This sends the SIGKILLsignal to the process, the effect of which is (very generally) that the OS immediately takes the process out of the run queue, and stops it. This can, in general, be somewhat dangerious, since it does not give the program the change to "clean up" or finish running in any kind of a reasonable way. It just literally stops it immediately.

这会向SIGKILL进程发送信号,其效果(非常普遍)是操作系统立即将进程从运行队列中取出并停止。一般来说,这可能有点危险,因为它不会让程序以任何合理的方式“清理”或完成运行。它只是立即停止它。

You can find more information about killon Wikipedia, or in the man pages(man killon your local machine). The command is fairly standard across Linux, UNIX, BSD, and OS X, so the documentation should more or less apply to any of these.

您可以killWikipedia手册页man kill在您的本地机器上)中找到有关更多信息。该命令在 Linux、UNIX、BSD 和 OS X 中是相当标准的,因此文档应该或多或少适用于其中任何一个。

Additionally, you can find more information about what the different signals are intended to do on Wikipediaas well.

此外,您还可以在Wikipedia上找到有关不同信号打算做什么的更多信息。

As an added bonus, I find that this songhas prevented me from ever forgetting about how this command works.

作为一个额外的好处,我发现这首歌让我永远不会忘记这个命令是如何工作的。

EDIT: Sometimes, there are processes that just cannot be killed. One of the most common situations in which this occurs is when the process is in the Dstate (see the STATcolumn in the output of ps axu). This state means that the process is uninterruptible, commonly because it's waiting for some I/O operation to finish that never will for various reasons. More information here.

编辑:有时,有些进程无法被杀死。发生这种情况的最常见情况之一是进程处于D状态时(请参阅STAT输出中的列ps axu)。这种状态意味着进程是不可中断的,通常是因为它正在等待某些 I/O 操作完成,但由于各种原因永远不会完成。更多信息在这里

Another note: sometimes killing a process doesn't work because you need to kill the parent process, not some worker process it's fork'ed. You can see the process "heirarchy" (i.e., the parent/child relationships) using the command ps axjf. You can see more information about the pscommand in the man page(man pson your local machine). As with kill, the pscommand is quite standard, so documentation should mostly apply to all of the various UNIX-like OS'es.

另一个注意事项:有时杀死进程不起作用,因为您需要杀死父进程,而不是某些工作进程fork。您可以使用命令查看进程“层次结构”(即父/子关系)ps axjf。您可以ps手册页man ps在您的本地机器上)中查看有关该命令的更多信息。与 一样kill,该ps命令是非常标准的,因此文档应该主要适用于所有各种类 UNIX 操作系统。

回答by Luke Singham

Alternatively, I found this answersolved the problem for me.

或者,我发现这个答案为我解决了这个问题。

Kill all processes named 'postgres'

杀死所有名为“postgres”的进程

pkill postgres