#!/sbin/runscript # Copyright 1999-2004 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # $Header: /var/cvsroot/gentoo-x86/media-sound/mpd/files/mpd-0.12.rc6,v 1.3 2006/10/03 19:26:17 ticho Exp $ depend() { need alsasound localmount use netmount nfsmount esound pulseaudio } # [2007-03-29, 21:11-01:36] - Zrajm C Akfohg modified this script so that it # kills MPD. This works even when MPD has hanged (which, for some reason, # happens to me quite frequently) while the traditional `mpd --kill' does not. # Usage: pschildren PID # # Lists process numbers of the immediate children of PID. Returns false if if # no children were found. /zrajm [2007-03-30] pschildren() { awk -F/ ' BEGIN { retval=1 } /^PPid: '"$1"'$/ { retval=0 $0=FILENAME print $3 nextfile } END { exit retval } ' /proc/[0-9]*/status # Listing `/proc/scsi' (at least under some cirkumstances) never terminates # on my system, so to avoid hanging this script, look only for "status" # files which begins with a *number* (i.e. is a PID) above. /zrajm # [2007-05-22] } # Usage: pstree PID # # Lists process number of PID, and all its offspring processes. # /zrajm [2007-03-30] pstree() { local PID for PID in "$@"; do echo "$PID" pstree $(pschildren "$PID") done } checkconfig() { if ! [ -f /etc/mpd.conf ]; then eerror "Configuration file /etc/mpd.conf does not exist." return 1 fi return 0 } start() { checkconfig || return 1 # You may set mpd's by setting "NICENESS" in `/etc/conf.d/mpd' (default: 0) # /zrajm [2007-06-04] local NICENESS="$(( ${NICENESS:-0} - $(nice) ))" ebegin "Starting Music Player Daemon" nice --adjustment=$NICENESS /usr/bin/mpd --no-create-db /etc/mpd.conf eend $? } stop() { ebegin "Stopping Music Player Daemon" [[ -r /etc/mpd.conf ]] && PIDFILE="$(awk -F'"' '/^ *pid_file/{ print $2; exit }' /etc/mpd.conf)" if [[ -r "$PIDFILE" ]]; then local PID KILLS="0" for PID in $(pstree "$(cat $PIDFILE)"); do [[ -d "/proc/$PID" ]] || continue einfo " Killing MPD process $PID." kill $PID && ((KILLS++)) done if (( KILLS > 1 )); then ewarn "NOTE: More than one MPD process was killed!" ewarn " (This normally does not happen, and usually means that" ewarn " the now stopped MPD instance was non-responsive/hanged.)" fi else /usr/bin/mpd --kill fi eend $? }