Add option to stop services in all launchd domains (#1077)

- Services started from Terminal run in the `gui/$uid`
  launchd domain. When `ensure-container-stopped.sh`
  runs from a different context, `launchctl managername`
  may return a different domain, causing the script to
  check the wrong domain and miss running services.
  I noticed this after upgrading `container` via Homebrew.
- Introduces a getopt option `-a` that allows booting out all
  domains explicitly. Also adds `-h` for a usage message.

Signed-off-by: Patrick Linnane <patrick@linnane.io>
This commit is contained in:
Patrick Linnane
2026-01-23 17:56:35 -08:00
committed by GitHub
parent 751c1fc05c
commit 14c279fcd2
+53 -13
View File
@@ -13,19 +13,59 @@
# See the License for the specific language governing permissions and
# limitations under the License.
domain_string=""
ALL_DOMAINS=false
launchd_domain=$(launchctl managername)
if [[ "$launchd_domain" == "System" ]]; then
domain_string="system"
elif [[ "$launchd_domain" == "Aqua" ]]; then
domain_string="gui/$(id -u)"
elif [[ "$launchd_domain" == "Background" ]]; then
domain_string="user/$(id -u)"
else
echo "Unsupported launchd domain. Exiting"
usage() {
echo "Usage: $0 [-a] [-h]"
echo "Stop container services"
echo
echo "Options:"
echo "a Stop container services in all launchd domains."
echo "h Show this help message."
echo
exit 1
fi
}
launchctl list | grep -e 'com\.apple\.container\W' | awk '{print $3}' | xargs -I % launchctl bootout $domain_string/%
while getopts ":ah" arg; do
case "$arg" in
a)
ALL_DOMAINS=true
;;
h)
usage
;;
*)
echo "Invalid option: -${OPTARG}"
usage
;;
esac
done
if $ALL_DOMAINS; then
uid=$(id -u)
for domain in "gui/$uid" "user/$uid" "system"; do
launchctl print "$domain" 2>/dev/null \
| grep -oE 'com\.apple\.container\.[^ ]+' \
| sort -u \
| while read -r service; do
launchctl bootout "$domain/$service"
done
done
else
domain_string=""
launchd_domain=$(launchctl managername)
if [[ "$launchd_domain" == "System" ]]; then
domain_string="system"
elif [[ "$launchd_domain" == "Aqua" ]]; then
domain_string="gui/$(id -u)"
elif [[ "$launchd_domain" == "Background" ]]; then
domain_string="user/$(id -u)"
else
echo "Unsupported launchd domain. Exiting"
exit 1
fi
launchctl list | grep -e 'com\.apple\.container\W' | awk '{print $3}' | xargs -I % launchctl bootout $domain_string/%
fi