如何停止已啟動的服務?

如何停止已啟動的服務?

我有一個標籤的啟動服務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

相關內容