我如何停止已启动的服务?

我如何停止已启动的服务?

我有一个带有标签的启动服务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

相关内容