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
Description=MPD to system shutdown
ExecStart=/bin/sh -c "sleep 10 && /usr/bin/inotifywait -e open /path/to/path/music/shutdown.mp3 && mpc clear && shutdown -h now"
You need: inotify-tools and mpc (in addition to mpd)
- 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.
- Then inotifywait will monitor shutdown.mp3 for being opened by mpd (or anything else). Once this happens inotifywait will quit successfully.
- 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.
- 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.