KEMBAR78
Allow systemctl stop on keep_alive services by using Restart=on-failure by Copilot · Pull Request #20799 · Homebrew/brew · GitHub
Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 2, 2025

Problem

When brew services generates systemd unit files with keep_alive true, it currently uses Restart=always. This prevents Linux users from manually stopping services with systemctl stop because systemd will always restart them immediately, even when explicitly stopped by the user.

Solution

Changed the systemd Restart policy from always to on-failure for keep_alive services. This provides the same automatic restart behavior on failures while allowing manual stops via systemctl to work as expected.

Implementation Details

  • keep_alive true and keep_alive always: true now generate Restart=on-failure instead of Restart=always
  • Implemented support for keep_alive crashed: trueRestart=on-failure (restart on non-zero exit, signals, timeouts)
  • Implemented support for keep_alive successful_exit: trueRestart=on-success (restart only on clean zero exit)

This aligns the systemd implementation with the existing macOS plist implementation, which already supports these different keep_alive options.

Benefits

  • Linux users can now use systemctl stop --user homebrew.SERVICENAME.service to temporarily stop a service without fully disabling it
  • Services still automatically restart on crashes and failures as intended
  • Better control over service lifecycle management on systemd-based systems

Example

Before this change, stopping a service would immediately restart it:

$ systemctl stop --user homebrew.myservice.service
# Service immediately restarts due to Restart=always

After this change:

$ systemctl stop --user homebrew.myservice.service
# Service stops and stays stopped until manually started or system reboot
# But will still auto-restart if it crashes or exits with a failure code

Closes #ISSUE_NUMBER

Original prompt

This section details on the original issue you should resolve

<issue_title>Allow systemctl stop on keep_alive services - Restart=on-failure (stale)</issue_title>
<issue_description>### Verification

Provide a detailed description of the proposed feature

When brew generates a systemd unit file with keep_alive true in the formula, it generates with Restart=always which prevents manually killing the service with systemctl directly. By switching to Restart=on-failure Linux users will be able to kill services with systemctl while still retaining a proper keep-alive functionality1. This would also limit the need for brew to handle generating multiple exit codes (I think it was done this way due to a limitation on the MacOS side).

I think this change only requires a single line be changed in the service.rb file but I'm not familiar enough the code to be sure.

options << "Restart=always" if @keep_alive.present? && @keep_alive[:always].present?

What is the motivation for the feature?

Being unable to kill a service with systemctl as a workaround to the brew services kill keep-alive limitation. The service only needed to be temporarily killed, not completely disabled, and being unable to manually kill it seemed a bit odd.

How will the feature be relevant to at least 90% of Homebrew users?

It will allow Linux users manually kill services with systemctl stop --user homebrew.SERVICENAME.service without needing to fully disable the service.

What alternatives to the feature have been considered?

I wasn't able to find any previous discussion but but an alternative would be to have brew handle multiple exit states within the keep-alive but I think this would diverge the Linux and MacOS side requiring quite a bit of change.</issue_description>

Comments on the Issue (you are @copilot in this section)

@MikeMcQuaid I don't think we should change the default behaviour at this point but I can see an argument for having a different DSL to accomplish what you'd want.

CC @SMillerDev for thoughts.</comment_new>
<comment_new>@SMillerDev
IIRC there is a different keepalive parameter for this, where it only keeps the service alive on failures.

I'm also not that excited about optimising something in brew service so that you can manage them outside of brew, since that's not really a goal for brew service in my opinion.</comment_new>
<comment_new>@SMillerDev

Well now I'm a bit confused. I went back and looked and found reference to different states under the Formula Cookbook but it looks like other states aren't implemented on a systemd generation? I'm like 99% sure I'm missing something here. I'll try testing it when I have sometime later today.

It is possible that I never implemented all options for systemd.</comment_new>
<comment_new>@MikeMcQuaid
@lethedata Thanks for the link! In that case: yes, this should definitely be implemented.</comment_new>
<comment_new>@MikeMcQuaid
@lethedata No, sorry. That's by design. If it seems like only one person cares: we'll probably let it get closed out and that person can create a PR if they desire.</comment_new>

Fixes #17639

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Footnotes

  1. systemd.service manual

@Copilot Copilot AI changed the title [WIP] Allow systemctl stop on keep_alive services - Restart=on-failure (stale) Allow systemctl stop on keep_alive services by using Restart=on-failure Oct 2, 2025
@Copilot Copilot AI requested a review from MikeMcQuaid October 2, 2025 16:20
Copilot finished work on behalf of MikeMcQuaid October 2, 2025 16:20
@MikeMcQuaid MikeMcQuaid force-pushed the copilot/fix-c94e3d65-4485-4e3a-a4ab-fbe172948495 branch from 70610b9 to 7f9d694 Compare October 3, 2025 14:40
Co-authored-by: MikeMcQuaid <125011+MikeMcQuaid@users.noreply.github.com>
@MikeMcQuaid MikeMcQuaid force-pushed the copilot/fix-c94e3d65-4485-4e3a-a4ab-fbe172948495 branch from 7f9d694 to 098a252 Compare October 3, 2025 15:19
@MikeMcQuaid MikeMcQuaid marked this pull request as ready for review October 3, 2025 15:19
@MikeMcQuaid MikeMcQuaid enabled auto-merge October 3, 2025 15:21
@MikeMcQuaid MikeMcQuaid added this pull request to the merge queue Oct 6, 2025
Merged via the queue into main with commit 86714e9 Oct 6, 2025
38 checks passed
@MikeMcQuaid MikeMcQuaid deleted the copilot/fix-c94e3d65-4485-4e3a-a4ab-fbe172948495 branch October 6, 2025 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow systemctl stop on keep_alive services - Restart=on-failure (stale)

3 participants