I'd like that when the program spotify-tui is closed, the service kill spotifyd. How would you write a service like that?

  • How is spotifyd started now? Does it have a unit file? Can you post its contents?
    – Seamus
    Dec 8, 2021 at 20:33
  • Right now it just starts on startup, from ~/.local/autorun Dec 8, 2021 at 20:35
  • 1
    To be honest, it doesn't sound to me that systemd is relevant / should be involved / considered at all. Some shell wrapper (that starts both spotifyd and spotify-tui) that makes use of shell trap to kill spotifyd upon exiting seems way more sensical for your case.
    – Tom Yan
    Dec 9, 2021 at 7:53
  • If you are not using systemd, it's safe to ignore the service and just run the script in background. But this question is about systemd. Dec 9, 2021 at 8:35

3 Answers 3


Essentially something like the following:

while true; do
  if ! pgrep spotify-tui; then
    pkill spotifyd
  sleep 10

then put into any background service you like.


You can use a watchdog to verify if the program freezes. Take a look in

systemd for Administrators, Part XV

  • 1
    Adding some relevant details from the link could be useful, especially if the link dies... Please edit and update your answer. Dec 9, 2021 at 2:44

This is how I created the service:

~/.local/share/systemd/spotifyd-autokill.service - calls the daemon

Description=spotifyd auto-killer (when spotify-tui is not running)

# Full path here is required


~/.local/share/systemd/user/daemons/spotifyd-autokill.sh - does the actual work


while true; do

  if `ps -ef | grep "alacritty --title spotify-tui" | awk {'print $2" "$8'} | grep -v grep > /dev/null`; then
    echo "Spotify-tui is running: No action taken."
    echo "Spotify-tui is not running: Closing spotifyd."
    killall -9 "spotifyd" 2> /dev/null
  sleep 120


~/.local/bin/spotify-tui - launches spotifyd when you open spotify-tui

alacritty --title 'spotify-tui' --config-file ~/.config/alacritty/monokai.yml -e zsh -c 'source ~/.zshrc && cd ~/workspaces/git-forks/spotify-tui && cargo run' &

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.