I have postgres and passenger installed from homebrew, and use the os x built in apache. I have several rails apps that use postgres which are managed by passenger which in turn is managed by apache.

Currently apache is started by launchd at boot from /System/Library/LaunchDaemons/org.apache.httpd.plist, apache starts passenger via /etc/apache2/other/passenger.conf and once I've logged in postgres is started via ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist.

Everything starts fine and works great until I need to shut down or reboot my mac; then I run into a problem with how launchd tears down everything. Basically Launchd sends SIGTERM to every user process and then 20 seconds later (http://www.opensource.apple.com/source/launchd/launchd-842.92.1/src/core.c) sends SIGKILL, but postgres treats SIGTERM as a "smart shutdown" signal (http://www.postgresql.org/docs/9.5/static/server-shutdown.html) and waits for all it's existing sessions to close themselves before quitting. The problem is that the sessions are owned by the rails apps running under passenger and therefore won't shut down until apache shuts down. And apache doesn't shutdown until the system is shutting down. Which means postgres gets SIGKILLed and leaves it's postmaster.pid file lying around which prevents postgres from starting up the next boot until I rm that file.

Would moving postgres's launchd plist to /Library/LaunchDaemons/ be a better idea? It would potentially make postgres and apache be shut down at the same time, which given 20 seconds to stop should be enough time for postgres to exit cleanly. But I could be missing an easier solution.

  • How long does it take for postgres to shut down if you do that leaving all the other processes running? Is the 20 seconds a guess or something that's pretty reliable for just that part of the puzzle? – bmike Mar 26 '16 at 16:00
  • postgres never shuts down if I just send it SIGTERM and leave all other processes the same. I tried that yesterday and left it for over an hour with no progress, but the second I restarted apache then postgres shutdown b/c all of the clients had shut down. – Camden Narzt Mar 26 '16 at 16:07
  • The 20 seconds is just how long launchctl waits by default before sending SIGKILL, but that's configurable. – Camden Narzt Mar 26 '16 at 16:08

I wound up moving the postgres launchd plist to /Library/LaunchDaemons and adding a UserName key/value pair (postgres won't run as root).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .