Postgresql – How to kill postgres processes that won’t die

postgresql

I have the following processes running per 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

I want them dead!

But killall postgres gives me:

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!

Best Solution

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

kill <pid>

This sends the SIGTERM signal to the process, telling them to stop.

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 kill command to do it a little (or, you know, a lot) more forcefully by doing:

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

This sends the SIGKILL signal 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.

You can find more information about kill on Wikipedia, or in the man pages (man kill on 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.

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

As an added bonus, I find that this song has 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 D state (see the STAT column 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.

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 ps command in the man page (man ps on your local machine). As with kill, the ps command is quite standard, so documentation should mostly apply to all of the various UNIX-like OS'es.

Related Question