Mac OSXでpg_ctl stopがPostgreSQLの停止に失敗したのでlaunchctl unloadした
$launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
- launchctl unload …
launchd
で管理しているサービス*1を停止するコマンド - -w … 自動起動しないように停止するオプション
- ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist …
PostgreSQL
をlaunchd
で管理するための設定ファイル
PostgreSQLデータベースをpg_ctl stop
しようとしたら、server does not shut down
とか言われて困ったので色々と調べたところ、launchctl unload
しろと書いてあるこちらの記事を見つけ、試したらすんなりと停止してくれました。
pg_ctl start
で起動させたPostgreSQLデータベースでも、何かの拍子にlaunchctl
の管理下に置かれることがあるようです。
邂逅
Railsアプリを作成していてデータベースの設定を変更したのでrake db:reset
しようとしたら、There is 1 other session using the database
と言われました。
$rake db:reset PG::ObjectInUse: ERROR: database "railsapp_development" is being accessed by other users DETAIL: There is 1 other session using the database. ・ ・ ・
データベースの利用に何か問題があるわけでは無さそうなので無視しようかとも思ったのですが、エラーを放置するのも気が引けたので、一旦、データベースを停止してみることにしました。
$pg_ctl stop waiting for server to shut down............................................................... failed pg_ctl: server does not shut down
server does not shut down
とか言われて、停止できませんでした(´・ω・`)
錯綜
とりあえずデータベースの状態でも確認してみようと考え、覚えてたコマンドを試してみました。
$pg_ctl status pg_ctl: server is running (PID: 34267) /usr/local/Cellar/postgresql/9.5.1/bin/postgres "-D" "/usr/local/var/postgres" "-r" "/usr/local/var/postgres/server.log"
server is running
の次の行に普段より長めの文字列が表示され*2、ちょっと面倒な気持ちになりながらも『server does not shut down』で検索すると、こちらでシャットダウンの方法に3つのモードがあることを知り、早速、-m fast
を試してみました。
$pg_ctl stop -m fast waiting for server to shut down............................................................... failed pg_ctl: server does not shut down
はい、ダメでしたヽ(・ω・)/
念のため、データディレクトリを明示的に指定したりもしましたが、やはりダメでした。
$pg_ctl stop -D /usr/local/var/postgres -m fast waiting for server to shut down............................................................... failed pg_ctl: server does not shut down
掃討
で、『server does not shut down』の検索を続けた後にStack Overflowの記事に辿り着きまして、そういえばbrew install
した時に最後の方にlaunchctl
云々とか書いてあったなと思い出し、
$brew install postgresql ・ ・ To load postgresql: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist Or, if you dont want/need launchctl, you can just run: postgres -D /usr/local/var/postgres ==> Summary 🍺 /usr/local/Cellar/postgresql/9.5.1: 3,118 files, 35M
launchctlに関する記事を読んでみたりして、これが原因の可能性が高そうだと思いhomebrew.mxcl.postgresql.plist
の中身を覗いてみました。
$more ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>KeepAlive</key> <true/> <key>Label</key> <string>homebrew.mxcl.postgresql</string> <key>ProgramArguments</key> <array> <string>/usr/local/opt/postgresql/bin/postgres</string> <string>-D</string> <string>/usr/local/var/postgres</string> <string>-r</string> <string>/usr/local/var/postgres/server.log</string> </array> <key>RunAtLoad</key> <true/> <key>WorkingDirectory</key> <string>/usr/local</string> <key>StandardErrorPath</key> <string>/usr/local/var/postgres/server.log</string> </dict> </plist>
<array>
の中の<string>
に見覚えのある文字列*3が記述されているのを発見し、原因の特定に近づいた確信を得たので、launchctl unload
を試してみました。
$launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
コマンドを入力しても何も表示されず不安になったので、状態を確認します。
$pg_ctl status
pg_ctl: no server running
止まってたー(*゚∀゚)
確認がてら、pg_ctl start
とpg_ctl stop
が動くか試してみます。
$pg_ctl start
server starting
$pg_ctl status pg_ctl: server is running (PID: 34960) /usr/local/Cellar/postgresql/9.5.1/bin/postgres
$pg_ctl stop waiting for server to shut down.... done server stopped
すんなり止まった…(゚Д゚)
泥濘
Stack Overflowの記事にはhomebrew.mxcl.postgresql.plist
の削除もするようにと書いてありましたが、今回は削除は保留して、いつか同じエラーに遭遇した時に削除しようと思います。
参考記事
- osx - PostgreSQL server wouldn't shutdown on Lion (Mac OS 10.7) - Stack Overflow
- Mac の launchctl ってなんだ? - IT戦記
- PostgreSQL のモード別終了方法 | Siguniang's Blog
開発環境
- OSX 10.11.4 El Capitan
- ターミナルエミュレータ: Macターミナル
- シェル: zsh
- パッケージマネージャ: Homebrew - Ruby 2.3.0
- バージョンマネージャ: rbenv
- Webフレームワーク: Ruby on Rails 4.2.6 - データベース
- ORDBMS: PostgreSQL 9.5.1
*1:ジョブとかプロセス
*2:普段は/usr/local/Cellar/postgresql/9.5.1/bin/postgresのみ表示されてた気がします
*3:pg_ctl statusで表示された文字列