Skip to content

Instantly share code, notes, and snippets.

@lrytz
Created September 20, 2014 12:49
Show Gist options
  • Save lrytz/8f9969eb0758bc1ab8bf to your computer and use it in GitHub Desktop.
Save lrytz/8f9969eb0758bc1ab8bf to your computer and use it in GitHub Desktop.
shairport init
#!/bin/sh
### BEGIN INIT INFO
# Provides: shairport
# Required-Start: $remote_fs $networking
# Required-Stop: $remote_fs $networking
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: shairport daemon
### END INIT INFO
#SCRIPT="/usr/local/bin/shairport -d -a catel -p 5003"
SCRIPT="/usr/local/bin/shairport -d -a catel"
RUNAS=shairport
# log in home directory since running as user 'shairport'
LOGFILE=/home/shairport/shairport.log
# regex to find existing prcess. letter in [u] to avoid grep finding its own process.
PIDREG='/[u]sr/local/bin/shairport '
start() {
if ps aux | grep "$PIDREG"; then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service…' >&2
killall -KILL pulseaudio
sleep 1
# fist start pulseaudio. shairport would start it anyway. but if we let shairport start
# it, we get cracking noise. not exactly sure what's the problem here.
su -c "/usr/bin/pulseaudio --start --log-target=syslog" $RUNAS
sleep 10
su -c "$SCRIPT &> \"$LOGFILE\" &" $RUNAS
echo 'Service started' >&2
}
stop() {
if ! ps aux | grep "$PIDREG"; then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping service…' >&2
# pkill will also kill child processes. the bonjour announcer is a separate task.
pkill -TERM -P `ps aux | grep "$PIDREG" | awk '{print $2}'`
echo 'Service stopped' >&2
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment