I'd like to mute my speaker (for a few seconds) after resuming from sleep, to avoid some annoying notification pings.
I have the following script in /lib/systemd/system-sleep/muteonresume
#!/bin/sh
PATH=/sbin:/usr/sbin:/bin:/usr/bin
case "$1" in
pre)
#code execution BEFORE sleeping/hibernating/suspending
touch /tmp/testpre;
;;
post)
#code execution AFTER resuming
if [ amixer get Master | grep "[on]" ]
then
# already muted
echo "already muted" > /tmp/testpost;
else
amixer set Master mute;
echo "I muted now" > /tmp/testpost;
fi
;;
esac
exit 0
The script is executed as expected, and puts "I muted now" in the log file /tmp/testpost. However, the speaker is not muted. Running amixer set Master mute standalone works as expected, both as myself and as root. But for some reason it does not work from this script. (Unless some other process unmutes after this happens, unbeknownst to me.)
Does anybody know why?
EDIT Given the suggestions in the comments, I changed my setup and it now includes two scripts.
/lib/systemd/system-sleep/muteonresume:
#!/bin/sh
PATH=/sbin:/usr/sbin:/bin:/usr/bin
case "$1" in
pre)
touch /tmp/mutelog;
;;
post)
amixer get Master | grep -qF '[on]' ;
ismuted=$?;
if [ $ismuted -eq 1 ]
then
echo date "already muted" >> /tmp/mutelog;
else
echo date "about to mute" >> /tmp/mutelog;
nohup /usr/local/bin/sleepmute.sh & disown
echo date "sent mute command" >> /tmp/mutelog;
fi
;;
esac
exit 0
and /usr/local/bin/sleepmute.sh
export XDG_RUNTIME_DIR=/run/user/$(id -u myusername)
export DISPLAY=:0
echo `date` "enter sleepmute.sh" >> /tmp/mutelog;
#sleep 5;
amixer set Master mute;
echo `date` "muted from script sleepmute.sh" >> /tmp/mutelog;
With this configuration, I can see the LED indicator on my mute button light up after resuming but it shortly turns off again. The mutelog file contains, in order: "about to mute", "sent mute command", "enter sleepmute.sh", "muted from script sleepmute.sh". As expected.
So it seems to do what it should but someone else is unmuting. To counteract that, I uncomment the sleep 5 in sleepmute.sh to make my script wait a bit before it mutes. But then "muted from script sleepmute.sh" is never printed to the log, only "enter sleepmute.sh". So even though I run the script with nohup, it seems to get killed before it can sleep 5 seconds and do the muting.