Skip to content

Commit

Permalink
Fix resh-daemon-stop fallbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
curusarn committed Feb 26, 2023
1 parent 30b254c commit 494f36c
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions scripts/resh-daemon-stop.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 494f36c

Please sign in to comment.