From 494f36cd1ce2d45d3eb282dfaa628598c67126c6 Mon Sep 17 00:00:00 2001 From: Simon Let Date: Sun, 26 Feb 2023 16:44:33 +0100 Subject: [PATCH] Fix resh-daemon-stop fallbacks --- scripts/resh-daemon-stop.sh | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/scripts/resh-daemon-stop.sh b/scripts/resh-daemon-stop.sh index 742c2d36..0f7c16e0 100755 --- a/scripts/resh-daemon-stop.sh +++ b/scripts/resh-daemon-stop.sh @@ -1,31 +1,34 @@ #!/usr/bin/env sh +set -eu -failed_to_kill() { - [ "${1-}" != "-q" ] && echo "Failed to kill the RESH daemon - it probably isn't running" -} +q=0 +[ "${1-}" != "-q" ] || q=1 xdg_pid() { local path="${XDG_DATA_HOME-}"/resh/daemon.pid [ -n "${XDG_DATA_HOME-}" ] && [ -f "$path" ] || return 1 cat "$path" + rm "$path" } default_pid() { local path=~/.local/share/resh/daemon.pid [ -f "$path" ] || return 1 cat "$path" + rm "$path" } -legacy_pid() { - local path=~/.resh/resh.pid - [ -f "$path" ] || return 1 - cat "$path" -} -pid=$(xdg_pid || default_pid || legacy_pid) -if [ -n "$pid" ]; then - [ "${1-}" != "-q" ] && printf "Stopping RESH daemon ... (PID: %s)\n" "$pid" - kill "$pid" || failed_to_kill -else - [ "${1-}" != "-q" ] && printf "Stopping RESH daemon ...\n" - killall -q resh-daemon || failed_to_kill -fi +kill_by_pid() { + [ -n "$1" ] || return 1 + [ "$q" = "1" ] || printf "Stopping RESH daemon ... (PID: %s)\n" "$1" + kill "$1" +} +kill_by_name() { + [ "$q" = "1" ] || printf "Stopping RESH daemon ...\n" + killall -q resh-daemon +} +failed_to_kill() { + [ "$q" = "1" ] || echo "Failed to kill the RESH daemon - it probably isn't running" + return 1 +} +kill_by_pid "$(xdg_pid)" || kill_by_pid "$(default_pid)" || kill_by_name || failed_to_kill