Shutting down the operating system by playing a “kill song” in MPD

I use a 20€ Dockstar with Archlinux ARM on it as MPD music server. Since I like to turn off electronic devices when they are not needed and pulling the plug is not something you should do to running computers, I was looking for a solution to somehow shut it down from any mpd client. So I added a specific song as “shutdown file”. Once that song is played, the system will cleanly shut down and power off.

Here is the systemd .service file I came up with:

# cat /etc/systemd/system/mpdtosystemshutdown.service
[Unit]
Description=MPD to system shutdown

[Service]
ExecStart=/bin/sh -c "sleep 10 && /usr/bin/inotifywait -e open /path/to/path/music/shutdown.mp3 && mpc clear && shutdown -h now"

[Install]
WantedBy=multi-user.target

You need: inotify-tools and mpc (in addition to mpd)

  1. The service sleeps for 10 seconds in the beginning because I keep the music on an external USB harddisk which gets mounted after the multi-user target is reached. This is an ugly workaround but should be good enough. A better solution would be using a udev rule or systemd mount unit to only start the service when the harddisk is mounted.
  2. Then inotifywait will monitor shutdown.mp3 for being opened by mpd (or anything else). Once this happens inotifywait will quit successfully.
  3. mpc will clear the mpd playlist as you do not want shutdown.mp3 to be played again right after booting, do you? I surely did not.
  4. Finally the system is being shut down and powered off.

You probably have to add shutdown.mp3 to your mpd database before starting this service or the database update might trigger it. Updating the database afterwards is save and does not trigger the shutdown.

Don’t blame me if this ends up with an infinite reboot loop.

2 thoughts on “Shutting down the operating system by playing a “kill song” in MPD

  1. Pingback: Raspberry Pi automatisch herunterfahren durch Spielen einer bestimmten Datei in MPD :) | blog.natenom.com

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.