launchd サービスを停止するにはどうすればいいですか?

launchd サービスを停止するにはどうすればいいですか?

ラベル付きの launchd サービスがありますhomebrew.mxcl.postgresql:

$ launchctl list homebrew.mxcl.postgresql
{
    "LimitLoadToSessionType" = "Aqua";
    "StandardErrorPath" = "/usr/local/var/log/postgres.log";
    "Label" = "homebrew.mxcl.postgresql";
    "TimeOut" = 30;
    "OnDemand" = false;
    "LastExitStatus" = 0;
    "PID" = 5436;
    "Program" = "/usr/local/opt/postgresql/bin/postgres";
    "ProgramArguments" = (
        "/usr/local/opt/postgresql/bin/postgres";
        "-D";
        "/usr/local/var/postgres";
    );
};

このサービスを停止したいのですが、明らかなstopコマンドは機能しません。代わりに、新しい PID でサービスを再起動します。

$ launchctl stop homebrew.mxcl.postgresql
$ launchctl list homebrew.mxcl.postgresql
{
    "LimitLoadToSessionType" = "Aqua";
    "StandardErrorPath" = "/usr/local/var/log/postgres.log";
    "Label" = "homebrew.mxcl.postgresql";
    "TimeOut" = 30;
    "OnDemand" = false;
    "LastExitStatus" = 0;
    "PID" = 5819;
    "Program" = "/usr/local/opt/postgresql/bin/postgres";
    "ProgramArguments" = (
        "/usr/local/opt/postgresql/bin/postgres";
        "-D";
        "/usr/local/var/postgres";
    );
};

実際にサービスを停止するにはどうすればいいですか?

答え1

停止したジョブは、開始されるはずであればすぐに再開されます。代わりに、サービスをアンロードする必要があります。

launchctl unload homebrew.mxcl.postgresql

永続的に無効にするには、-wスイッチも指定します。

launchctl unload -w homebrew.mxcl.postgresql

Homebrewの場合は、自作サービスインストールするには:

brew tap homebrew/services

すると、サービスの開始と停止がより簡単になります。

brew services start postgresql
brew services stop postgresql

関連情報