Skip to content

Instantly share code, notes, and snippets.

@Sohalt
Created January 11, 2019 11:50
Show Gist options
  • Save Sohalt/4ca2a70f59bbda718f052436da844558 to your computer and use it in GitHub Desktop.
Save Sohalt/4ca2a70f59bbda718f052436da844558 to your computer and use it in GitHub Desktop.
suspends the computer using systemctl suspend when music on mpd is paused, needs mpc
#!/bin/bash
#suspends the computer when mpd gets paused
if ! command -v > /dev/null mpc
then
echo "error: this needs mpc to be installed"
exit 1
fi
uid=$(id -u)
pidfile="/var/run/user/$uid/mpd-suspend-on-pause.pid"
if [[ "$1" == "-k" ]] # kill
then
if [[ -f $pidfile ]]
then
(kill "$(cat $pidfile)" 2> /dev/null) || (>&2 echo "not running";exit 1)
fi
elif [[ "$1" == "-s" ]] # status
then
if [[ -f $pidfile ]] && ps $(cat $pidfile) > /dev/null;
then
echo "running"
else
echo "not running"
fi
else
if [[ -f $pidfile ]] && ps $(cat $pidfile) > /dev/null;
then
(>&2 echo "already running")
exit 1
else
while (true)
do
mpc idle > /dev/null
if [[ ! $(mpc status | grep playing) ]]
then
systemctl suspend -i
break
fi
done &
echo "$!" > $pidfile
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment