MPD以外のサービスを停止する
さて、今回はヤバ目のネタを上げていきます。
**警告**
今回のネタは純正の各サービスのイベント起動用シェルスクリプトの書換を行うというものです。
書換に失敗した場合、最悪不要なサービスの多重起動を起こし正常な動作を阻害しますのでくれぐれも注意して実行+自己責任で行って下さい。
少なくともUNIX系OSのシェル操作に慣れていない人はやめたほうが良いと思います。
-
はじめに
symphonic-mpdでは大きく分けて、MPD・AirPlay・spotifyでの再生の3つの機能を使用することが出来ます。
各サービスは排他的(1つのサービスを使用時に他のサービスは停止状態となる)動作となりますが、このディストリでパパリウスさまの基本理念と同じく「必要としないサービス、TCP/UDPポートは閉じてしまい音質を向上させる」の考えに基づいて予めAirPlay・spotifyのサービスを動かなくしてしまうというのが今回の方法です。
(と、大げさな理由を書いておりますが、本当のところAirPlayはApple系のサービスでこれだけ有効なのは個人的に嫌だと言うのが本音。アップルファンの人ゴメンナサイ)
-
無効化の概要
大きく分けて2つのことをする必要があります。
◯起動時に使用したくないサービスを初めから動作させない
◯他のサービスの終了などのトリガーで使用したくないサービスが立ち上がるのを防止する。
というわけで、以下でやり方を紹介します。
-
新規作成するファイル
/home/pi/configs/ 下に以下のファイルを作成して下さい2019-10-13 23:01 ファイルアップロード対応。
shairport_disable.sh
#!/bin/sh
FLGDIR=/home/pi/configs
if [ -e $FLGDIR/shairport.flg ]; then
shair=0
else
shair=1
fi
case "$1" in
start)
if [ $shair -eq 0 ]; then
echo "shairport-sync is already DISABLED"
else
if [ ! "`pidof shairport-sync`" = "" ]; then
sudo systemctl stop shairport-sync.service
fi
sudo systemctl disable shairport-sync.service
sudo systemctl mask shairport-sync.service
touch $FLGDIR/shairport.flg
echo "done! shairport-sync service is now disabled"
#echo "If you want to take this config effect, please reboot!!"
fi
;;
stop)
if [ $shair -eq 0 ]; then
echo "enabling shairport_sync..."
rm $FLGDIR/shairport.flg
sudo systemctl unmask shairport-sync.service
sudo systemctl enable shairport-sync.service
sudo systemctl start shairport-sync.service
echo "done! shairport-sync service is now enabled"
#echo "If you want to take this config effect, please reboot!!"
else
echo "shairport_sync is already ENABLED"
fi
;;
status)
if [ $shair -eq 0 ]; then
echo "shairport-sync has been DISABLED by default"
else
echo "shairport-sync has been ENABLED by default"
fi
;;
esac
exit 0
ファイル作成後 chmod +x shairport_disable.sh
で実行権限を追加して下さい。
spotify_disable.sh
#!/bin/sh
FLGDIR=/home/pi/configs
if [ -e $FLGDIR/spotifyd.flg ]; then
spot=0
else
spot=1
fi
case "$1" in
start)
if [ $spot -eq 0 ]; then
echo "spotifyd already DISABLED!!"
else
echo "disabling spotifyd..."
if [ ! "`pidof spotifyd`" = "" ]; then
sudo systemctl stop spotifyd.service
fi
sudo systemctl disable spotifyd.service
sudo systemctl mask spotifyd.service
touch $FLGDIR/spotifyd.flg
echo "done! spotifyd service is now Disabled"
#echo "If you want to take this config effect, please reboot!!"
fi
;;
stop)
if [ $spot -eq 0 ]; then
echo "enabing spotifyd..."
rm $FLGDIR/spotifyd.flg
sudo systemctl unmask spotifyd.service
sudo systemctl enable spotifyd.service
sudo systemctl start spotifyd.service
echo "done! spotifyd serice is now Enabled"
#echo "If you want to take this config effect, please reboot!!"
else
echo "spotifyd is already ENABLED!!"
fi
;;
status)
if [ $spot -eq 0 ]; then
echo "spotifyd has been DISABLED by default"
else
echo "spotifyd has been ENABLED by default"
fi
;;
esac
exit 0
これもファイル作成後 chmod +x spotify_disable.sh
で実行権限を追加して下さい。
後述しますが、これら2つのファイルはshairport-syncとspotifydを無効化するためのフラグファイルを作成+各デーモンのsystemdスクリプトを/etcから消すためのものです。
- 書き換えするファイル(/home/pi/configs 配下)
shairport_event.sh
#!/bin/bash
FLGDIR=/home/pi/configs
if [ -e $FLGDIR/spotifyd.flg ]; then
spot=0
else
spot=1
fi
case "$1" in
begin)
echo "$DEFAULT_SAMPLE_FORMAT 44100 2 AIRPLAY" > /run/pcminfo
sudo systemctl restart pipe
# stop mpd
sudo systemctl stop ympd
sudo systemctl stop ympd.socket
sudo systemctl stop mpd
sudo umount /var/lib/mpd/music/NAS
# stop spotify-connect
if [ $spot -eq 1 ]; then
sudo systemctl stop spotifyd
sync
sleep 0.4
fi
exit
;;
end)
# wakeup mpd
sudo systemctl start ympd.socket
sudo mount -a
sudo systemctl start mpd
# wakeup spotify-connect
if [ $spot -eq 1 ]; then
sudo systemctl start spotifyd
sync
fi
exit
;;
esac
exit 0
spotify_event.sh
#!/bin/bash
FLGDIR=/home/pi/configs
if [ -e $FLGDIR/shairport.flg ]; then
shair=0
else
shair=1
fi
pcminfo=/run/pcminfo
case "$PLAYER_EVENT" in
start)
# stop playback
/usr/bin/mpc stop > /dev/null
# stop mpd
sudo systemctl stop ympd
sudo systemctl stop ympd.socket
sudo systemctl stop mpd
sudo umount /var/lib/mpd/music/NAS
# stop shairport-sync
if [ $shair -eq 1 ]; then
sudo systemctl stop shairport-sync
sudo sync
sleep 0.4
fi
exit
;;
stop)
if [ ! "`cat /run/pcminfo |cut -d ' ' -f4`" = "DEFAULT" ];then
exit
fi
# wakeup mpd
sudo systemctl start ympd.socket
sudo mount -a
sudo systemctl start mpd
# start shairport-sync
if [ $shair -eq 1 ]; then
sudo systemctl start shairport-sync
sudo sync
fi
exit
;;
esac
exit 0
これらのファイルはshairport-sync、spotifydで何らかのイベントが発生した場合(再生開始時や終了時など)に他のサービスを停止させたり復活させる記述が書かれています。
- 書き換えするファイル(/home/pi/misc 配下)
※2019-11-21 修正の上、ファイル差替
ympd_event.sh
#!/bin/bash
FLGDIR=/home/pi/configs
if [ -e $FLGDIR/shairport.flg ]; then
shair=0
else
shair=1
fi
if [ -e $FLGDIR/spotifyd.flg ]; then
spot=0
else
spot=1
fi
case "$1" in
isolate)
if [ $spot -eq 1 ]; then
systemd-run --nice=19 systemctl stop spotifyd
fi
if [ $shair -eq 1 ]; then
systemd-run --nice=19 systemctl stop shairport-sync
fi
sleep 0.4
;;
stop-avahi)
systemd-run --nice=19 systemctl stop avahi-daemon
systemd-run --nice=19 systemctl stop avahi-daemon.socket
sleep 0.4
;;
pause)
if [ "`pidof avahi-daemon`" = "" ]; then
systemctl start avahi-daemon.socket
systemctl start avahi-daemon
fi
if [ "`pidof shairport-sync`" = "" ] && [ $shair -eq 1 ]; then
systemctl start shairport-sync
fi
if [ "`pidof spotifyd`" = "" ] && [ $spot -eq 1 ]; then
systemd-run /home/pi/misc/ympd_event.sh spotifyd-start
fi
;;
spotifyd-start)
sleep 2
if [ $spot -eq 1 ]; then
systemctl start spotifyd
fi
;;
stop)
if [ $(mpc|grep "[playing]"|wc -l) = 1 -o $(mpc|grep "[paused]"|wc -l) = 1 ]; then
exit
fi
if [ "`pidof avahi-daemon`" = "" ]; then
systemctl start avahi-daemon.socket
systemctl start avahi-daemon
fi
if [ "`pidof shairport-sync`" = "" ] && [ $shair -eq 1 ]; then
systemctl start shairport-sync
fi
if [ "`pidof spotifyd`" = "" ] && [ $spot -eq 1]; then
systemd-run /home/pi/misc/ympd_event.sh spotifyd-start
fi
;;
browse)
ls /var/lib/mpd/music/RAM/* >/dev/null 2>&1
if [ $? -eq 0 ]; then
mpc update RAM
fi
;;
dashboard)
/home/pi/util-plot.sh
/home/pi/util-dashboard.sh
;;
esac
exit
このファイルはympdのイベント発生時(MPDにて音源の再生・停止時など)に他のサービスを停止・起動させる記述が書かれています。
なお、ファイルを書替する場合は、必ず念の為元のファイルをリネームして保存しておいて下さい。
- 使い方
音声を鳴らしていない状況でラズパイにsshでリモートシェルに接続し、/home/pi/configs 配下でshairport_disable.sh・spotify_disable.shを以下の通り実行します。
AirPlayを無効化する場合(shairport-syncを無効化)
sudo ./shairport_disable.sh start
spotifyを使わない場合(spotifydを無効化)
sudo ./spotify_disable.sh start
無効化時は以下の動作を行います(これによりイベントでそれぞれのサービスを起動させなくする)。
既に起動中の場合はshairport-sync/spotifydのデーモンを停止する
shairport.flg/spotify.flgという名前の空ファイルを生成(この空ファイルがある場合、shairport-sync・spotifydは起動しないよう各イベントのシェルスクリプトを改変済み)
/etc/systemd/system 配下の shairport-sync.service spotifyd.service を無効化
それぞれ必要なサービスの無効化を行った後は、本体の再起動をして下さい。
次に上記の方法で無効化したが元に戻したい場合
AirPlayを復活させる場合(shairport-syncを有効化)
sudo ./shairport_disable.sh stop
spotifyを復活させる場合(spotifydを有効化)
sudo ./spotify_disable.sh stop
無効化の解説の逆のことを行っています。無効化時と同じくシェルスクリプト使用後は本体の再起動を行って下さい音声再生を行っていない状況ならば無効化設定・再有効化ともに可能です。ただし精神衛生上不安を感じるならば再起動をお勧めします。
- 効果の程は?
AirPortを有効化した場合、5000番ポートがListen(開きっぱなし)になります。無効化すれば当然このポートは閉じる状態となります。
私の場合MPDでPCクライアントを使用するため6600番ポートが常時空いているため、効果は相殺されるかもしれません。