homebrew-services icon indicating copy to clipboard operation
homebrew-services copied to clipboard

brew services restart (stop/start) via ssh does not return and loops in /bin/launchctl list

Open logopk opened this issue 2 years ago • 4 comments

When I ssh into a remote mac brew services restart does not work.

Apparently since end of September with the introduction of systemctl and the warning about user/* and gui/* domains(https://github.com/Homebrew/homebrew-services/commit/9f7d5758d84906e59049c1366a5a0b685016e8a7)

brew services restart node_exporter --debug only shows a repeated

▶ brew services stop node_exporter --debug
/usr/local/Homebrew/Library/Homebrew/brew.rb (Formulary::FormulaAPILoader): loading node_exporter from API
==> /bin/launchctl list homebrew.mxcl.node_exporter
{
	"StandardOutPath" = "/usr/local/var/log/node_exporter.log";
	"LimitLoadToSessionType" = "Aqua";
	"StandardErrorPath" = "/usr/local/var/log/node_exporter.err.log";
	"Label" = "homebrew.mxcl.node_exporter";
	"OnDemand" = true;
	"LastExitStatus" = 0;
	"PID" = 40425;
	"Program" = "/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	"ProgramArguments" = (
		"/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	);
};
Stopping `node_exporter`... (might take a while)
Warning: running over SSH, using user/* instead of gui/* domain!
Hide this warning by setting HOMEBREW_SERVICES_NO_DOMAIN_WARNING.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> /bin/launchctl list homebrew.mxcl.node_exporter
{
	"StandardOutPath" = "/usr/local/var/log/node_exporter.log";
	"LimitLoadToSessionType" = "Aqua";
	"StandardErrorPath" = "/usr/local/var/log/node_exporter.err.log";
	"Label" = "homebrew.mxcl.node_exporter";
	"OnDemand" = true;
	"LastExitStatus" = 0;
	"PID" = 40425;
	"Program" = "/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	"ProgramArguments" = (
		"/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	);
};
==> /bin/launchctl list homebrew.mxcl.node_exporter
{
	"StandardOutPath" = "/usr/local/var/log/node_exporter.log";
	"LimitLoadToSessionType" = "Aqua";
	"StandardErrorPath" = "/usr/local/var/log/node_exporter.err.log";
	"Label" = "homebrew.mxcl.node_exporter";
	"OnDemand" = true;
	"LastExitStatus" = 0;
	"PID" = 40425;
	"Program" = "/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	"ProgramArguments" = (
		"/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	);
};
....loooooop

Any idea why and how I can further troubleshoot this?

Native on the machine it works without a problem.

▶ brew config
HOMEBREW_VERSION: 4.1.20-11-g6de29c2
ORIGIN: https://github.com/Homebrew/brew
HEAD: 6de29c2f65c909bcc38432feafdaa71322d8bf78
Last commit: 12 hours ago
Core tap origin: https://github.com/Homebrew/homebrew-core
Core tap HEAD: ec3a0feb573154833c186388cbd3f8b11d213813
Core tap last commit: 8 hours ago
Core tap branch: master
Core tap JSON: 14 Nov 08:15 UTC
HOMEBREW_PREFIX: /usr/local
HOMEBREW_CASK_OPTS: []
HOMEBREW_GITHUB_API_TOKEN: set
HOMEBREW_MAKE_JOBS: 8
HOMEBREW_SORBET_RUNTIME: set
Homebrew Ruby: 2.6.10 => /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.6.10_1/bin/ruby
CPU: octa-core 64-bit haswell
Clang: 13.0.0 build 1300
Git: 2.42.1 => /usr/local/bin/git
Curl: 8.1.2 => /usr/bin/curl
macOS: 12.7.1-x86_64
CLT: 14.2.0.0.1.1668646533
Xcode: 13.2.1
▶ brew services restart node_exporter --debug
/usr/local/Homebrew/Library/Homebrew/brew.rb (Formulary::FormulaAPILoader): loading node_exporter from API
==> /bin/launchctl list homebrew.mxcl.node_exporter
{
	"StandardOutPath" = "/usr/local/var/log/node_exporter.log";
	"LimitLoadToSessionType" = "Aqua";
	"StandardErrorPath" = "/usr/local/var/log/node_exporter.err.log";
	"Label" = "homebrew.mxcl.node_exporter";
	"OnDemand" = true;
	"LastExitStatus" = 0;
	"PID" = 10832;
	"Program" = "/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	"ProgramArguments" = (
		"/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	);
};
==> /bin/launchctl list homebrew.mxcl.node_exporter
{
	"StandardOutPath" = "/usr/local/var/log/node_exporter.log";
	"LimitLoadToSessionType" = "Aqua";
	"StandardErrorPath" = "/usr/local/var/log/node_exporter.err.log";
	"Label" = "homebrew.mxcl.node_exporter";
	"OnDemand" = true;
	"LastExitStatus" = 0;
	"PID" = 10832;
	"Program" = "/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	"ProgramArguments" = (
		"/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	);
};
==> /bin/launchctl list homebrew.mxcl.node_exporter
{
	"StandardOutPath" = "/usr/local/var/log/node_exporter.log";
	"LimitLoadToSessionType" = "Aqua";
	"StandardErrorPath" = "/usr/local/var/log/node_exporter.err.log";
	"Label" = "homebrew.mxcl.node_exporter";
	"OnDemand" = true;
	"LastExitStatus" = 0;
	"PID" = 10832;
	"Program" = "/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	"ProgramArguments" = (
		"/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	);
};
Stopping `node_exporter`... (might take a while)
==> /bin/launchctl print gui/501/homebrew.mxcl.node_exporter

==> /bin/launchctl print gui/501/homebrew.mxcl.node_exporter

==> Successfully stopped `node_exporter` (label: homebrew.mxcl.node_exporter)
==> Successfully started `node_exporter` (label: homebrew.mxcl.node_exporter)
``

logopk avatar Nov 14 '23 14:11 logopk

Once I replace

"user/#{Process.uid}"

with

"gui/#{Process.uid}" in homebrew-services/lib/service/system.rb everything works... :-)

logopk avatar Nov 14 '23 15:11 logopk

"gui/#{Process.uid}" in homebrew-services/lib/service/system.rb everything works... :-)

This will only work if a user is already logged in. That said: I think it would be a good idea to make this behaviour either configurable or detect the cases like yours in which it could/should work.

Will review PRs!

MikeMcQuaid avatar Nov 14 '23 15:11 MikeMcQuaid

AFAIK User-Services will run only if a user has been logged in once. This works in my case for gitlab-runner and node-exporter (as LauchAgents).

rsyslogd runs as root without login (as LaunchDaemon)

logopk avatar Nov 14 '23 16:11 logopk

@logopk need to figure out a way to detect that from code.

MikeMcQuaid avatar Nov 14 '23 16:11 MikeMcQuaid

When I ssh into a remote mac brew services restart does not work.

Apparently since end of September with the introduction of systemctl and the warning about user/* and gui/* domains(https://github.com/Homebrew/homebrew-services/commit/9f7d5758d84906e59049c1366a5a0b685016e8a7)

brew services restart node_exporter --debug only shows a repeated

▶ brew services stop node_exporter --debug
/usr/local/Homebrew/Library/Homebrew/brew.rb (Formulary::FormulaAPILoader): loading node_exporter from API
==> /bin/launchctl list homebrew.mxcl.node_exporter
{
	"StandardOutPath" = "/usr/local/var/log/node_exporter.log";
	"LimitLoadToSessionType" = "Aqua";
	"StandardErrorPath" = "/usr/local/var/log/node_exporter.err.log";
	"Label" = "homebrew.mxcl.node_exporter";
	"OnDemand" = true;
	"LastExitStatus" = 0;
	"PID" = 40425;
	"Program" = "/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	"ProgramArguments" = (
		"/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	);
};
Stopping `node_exporter`... (might take a while)
Warning: running over SSH, using user/* instead of gui/* domain!
Hide this warning by setting HOMEBREW_SERVICES_NO_DOMAIN_WARNING.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> /bin/launchctl list homebrew.mxcl.node_exporter
{
	"StandardOutPath" = "/usr/local/var/log/node_exporter.log";
	"LimitLoadToSessionType" = "Aqua";
	"StandardErrorPath" = "/usr/local/var/log/node_exporter.err.log";
	"Label" = "homebrew.mxcl.node_exporter";
	"OnDemand" = true;
	"LastExitStatus" = 0;
	"PID" = 40425;
	"Program" = "/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	"ProgramArguments" = (
		"/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	);
};
==> /bin/launchctl list homebrew.mxcl.node_exporter
{
	"StandardOutPath" = "/usr/local/var/log/node_exporter.log";
	"LimitLoadToSessionType" = "Aqua";
	"StandardErrorPath" = "/usr/local/var/log/node_exporter.err.log";
	"Label" = "homebrew.mxcl.node_exporter";
	"OnDemand" = true;
	"LastExitStatus" = 0;
	"PID" = 40425;
	"Program" = "/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	"ProgramArguments" = (
		"/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	);
};
....loooooop

Any idea why and how I can further troubleshoot this?

Native on the machine it works without a problem.

▶ brew config
HOMEBREW_VERSION: 4.1.20-11-g6de29c2
ORIGIN: https://github.com/Homebrew/brew
HEAD: 6de29c2f65c909bcc38432feafdaa71322d8bf78
Last commit: 12 hours ago
Core tap origin: https://github.com/Homebrew/homebrew-core
Core tap HEAD: ec3a0feb573154833c186388cbd3f8b11d213813
Core tap last commit: 8 hours ago
Core tap branch: master
Core tap JSON: 14 Nov 08:15 UTC
HOMEBREW_PREFIX: /usr/local
HOMEBREW_CASK_OPTS: []
HOMEBREW_GITHUB_API_TOKEN: set
HOMEBREW_MAKE_JOBS: 8
HOMEBREW_SORBET_RUNTIME: set
Homebrew Ruby: 2.6.10 => /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.6.10_1/bin/ruby
CPU: octa-core 64-bit haswell
Clang: 13.0.0 build 1300
Git: 2.42.1 => /usr/local/bin/git
Curl: 8.1.2 => /usr/bin/curl
macOS: 12.7.1-x86_64
CLT: 14.2.0.0.1.1668646533
Xcode: 13.2.1
▶ brew services restart node_exporter --debug
/usr/local/Homebrew/Library/Homebrew/brew.rb (Formulary::FormulaAPILoader): loading node_exporter from API
==> /bin/launchctl list homebrew.mxcl.node_exporter
{
	"StandardOutPath" = "/usr/local/var/log/node_exporter.log";
	"LimitLoadToSessionType" = "Aqua";
	"StandardErrorPath" = "/usr/local/var/log/node_exporter.err.log";
	"Label" = "homebrew.mxcl.node_exporter";
	"OnDemand" = true;
	"LastExitStatus" = 0;
	"PID" = 10832;
	"Program" = "/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	"ProgramArguments" = (
		"/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	);
};
==> /bin/launchctl list homebrew.mxcl.node_exporter
{
	"StandardOutPath" = "/usr/local/var/log/node_exporter.log";
	"LimitLoadToSessionType" = "Aqua";
	"StandardErrorPath" = "/usr/local/var/log/node_exporter.err.log";
	"Label" = "homebrew.mxcl.node_exporter";
	"OnDemand" = true;
	"LastExitStatus" = 0;
	"PID" = 10832;
	"Program" = "/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	"ProgramArguments" = (
		"/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	);
};
==> /bin/launchctl list homebrew.mxcl.node_exporter
{
	"StandardOutPath" = "/usr/local/var/log/node_exporter.log";
	"LimitLoadToSessionType" = "Aqua";
	"StandardErrorPath" = "/usr/local/var/log/node_exporter.err.log";
	"Label" = "homebrew.mxcl.node_exporter";
	"OnDemand" = true;
	"LastExitStatus" = 0;
	"PID" = 10832;
	"Program" = "/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	"ProgramArguments" = (
		"/usr/local/opt/node_exporter/bin/node_exporter_brew_services";
	);
};
Stopping `node_exporter`... (might take a while)
==> /bin/launchctl print gui/501/homebrew.mxcl.node_exporter

==> /bin/launchctl print gui/501/homebrew.mxcl.node_exporter

==> Successfully stopped `node_exporter` (label: homebrew.mxcl.node_exporter)
==> Successfully started `node_exporter` (label: homebrew.mxcl.node_exporter)
``


oasissoman avatar Feb 26 '24 14:02 oasissoman

When I ssh into a remote mac brew services restart does not work.

We would need to add an option to allow you to force the usage of gui/* in that case. You cannot start services that way reliably unless the same user is already logged into a GUI session.

Again: this could be perhaps be detected automatically and handled via code.

MikeMcQuaid avatar Feb 27 '24 08:02 MikeMcQuaid

We would need to add an option to allow you to force the usage of gui/* in that case. You cannot start services that way reliably unless the same user is already logged into a GUI session.

Again: this could be perhaps be detected automatically and handled via code.

Hi @MikeMcQuaid and thanks for the amazing software. I kinda have the same issue when I ssh into my other Mac where I have local taps with brew services configured. Should it be possible to add maybe a ENV var that handle this?

Maybe something like this: (using a ENV var HOMEBREW_FORCE_SSH_GUI)

def domain_target
  if root?
    "system"
  elsif ((ssh_tty = ENV.fetch("HOMEBREW_SSH_TTY", nil).present?) || ENV.fetch("HOMEBREW_SUDO_USER", nil).present?)
 && ENV.fetch("HOMEBREW_FORCE_SSH_GUI", nil).nil?
 
    if @output_warning.blank? && ENV.fetch("HOMEBREW_SERVICES_NO_DOMAIN_WARNING", nil).blank?
      if ssh_tty
        opoo "running over SSH, using user/* instead of gui/* domain!"
      else
        opoo "running through sudo, using user/* instead of gui/* domain!"
      end
      unless Homebrew::EnvConfig.no_env_hints?
        puts "Hide this warning by setting HOMEBREW_SERVICES_NO_DOMAIN_WARNING."
        puts "Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`)."
      end
      @output_warning = true
    end
    "user/#{Process.uid}"
  else
    "gui/#{Process.uid}"
  end
end

(Sorry but I don’t know Ruby that much)

kevinm6 avatar Feb 27 '24 08:02 kevinm6

When I ssh into a remote mac brew services restart does not work.

We would need to add an option to allow you to force the usage of gui/* in that case. You cannot start services that way reliably unless the same user is already logged into a GUI session.

Again: this could be perhaps be detected automatically and handled via code.

@MikeMcQuaid Thanks for your support. Just to clarify: I am logged in (with the same user) on the remote machine. When I ssh into the machine from remote (VPN or so) the problem starts.

logopk avatar Feb 27 '24 08:02 logopk

بمجرد استبدال

"user/#{Process.uid}"

مع.

"gui/#{Process.uid}"في homebrew-services/lib/service/system.rb كل شيء يعمل...:-)

https://github.com/Homebrew/homebrew-core

oasissoman avatar Mar 03 '24 11:03 oasissoman

Have opened https://github.com/Homebrew/homebrew-services/pull/638 to fix this. Would appreciate testing!

MikeMcQuaid avatar Mar 28 '24 18:03 MikeMcQuaid

@MikeMcQuaid just tested the new branch with my usage and it’s working as expected. Thanks

kevinm6 avatar Mar 28 '24 19:03 kevinm6

@kevinm6 Great, thanks for testing!

MikeMcQuaid avatar Mar 29 '24 08:03 MikeMcQuaid