Running subsonic standalone as daemon automatically on Linux
Posted: Sun Mar 08, 2009 3:39 pm
Hi all
I realized shortly after completing this that wintermute already posted a bash-script for starting subsonic. However personally I prefer to have all services running as dedicated users to prevent any security breaches and such on my server. No offense to wintermute
Here's at least how Subsonic is handled by my system, free for anybody to use
Subsonic is started automatically by init at boot time on runlevels 3-5 run as the user subsonic
This is done without modifying the original subsonic.sh at all to avoid having to do updates after upgrading Subsonic
PS: However some changes to subsonic.sh could improve the init-script somewhat
init-script put in /etc/init.d/subsonic
To setup the subsonic user and register the init-script
Example of usage
I realized shortly after completing this that wintermute already posted a bash-script for starting subsonic. However personally I prefer to have all services running as dedicated users to prevent any security breaches and such on my server. No offense to wintermute
Here's at least how Subsonic is handled by my system, free for anybody to use
Subsonic is started automatically by init at boot time on runlevels 3-5 run as the user subsonic
This is done without modifying the original subsonic.sh at all to avoid having to do updates after upgrading Subsonic
PS: However some changes to subsonic.sh could improve the init-script somewhat
init-script put in /etc/init.d/subsonic
- Code: Select all
#!/bin/sh
#
# chkconfig: 345 99 05
# /etc/init.d/subsonic
#
# Description: Linux startup-script for Subsonic standalone
#
[ ! -d /usr/bin ] && exit
# On RedHat AS 3 and above, use runuser rather than su
SU="su"
if [ -f "/etc/redhat-release" -a -x "/sbin/runuser" ]; then
SU="runuser"
fi
if [ -f "/etc/redhat-release" ]; then
LOCKFILE=/var/lock/subsys/subsonic
fi
PATH="$PATH:/bin:/usr/bin"
export PATH
SUBSONIC_USERNAME=subsonic
SERVICENAME="Subsonic Media Streamer"
SUBSONIC_HOME="/var/subsonic/"
# Source function library.
. /etc/rc.d/init.d/functions
# Functions for starting and stopping subsonic
start() {
echo -n $"Starting $SERVICENAME: "
# Start subsonic and pipe any output to syslog ( then there's no need to modify subsonic.sh at all )
$SU - "$SUBSONIC_USERNAME" -s /bin/sh -c "$SUBSONIC_HOME/subsonic.sh" | logger -p daemon.info -t subsonic
# Check if subsonic started since we are not getting RETVAL from subsonic.sh
getpid
if [ $PID ]; then
success && RETVAL=0
else
failure && RETVAL=1
fi
echo
[ $RETVAL = 0 ] && touch $LOCKFILE
return $RETVAL
}
stop() {
echo -n "Stopping $SERVICENAME: "
kill $PID
# Check if subsonic terminated since we are not getting RETVAL from kill ( killproc with pidfile would solve this )
sleep 1
getpid
if [ $PID ]; then
failure && RETVAL=1
else
success && RETVAL=0
fi
echo
[ $RETVAL = 0 ] && rm -f $LOCKFILE
return $RETVAL
}
status() {
if [ $PID ]; then
echo -n "$SERVICENAME is running.. Found PID $PID"
else
echo -n "$SERVICENAME is not running.."
fi
echo
}
getpid() {
# Using a pidfile would be better, but would have to modify subsonic.sh to do that, so for now..
PID=`ps -u $SUBSONIC_USERNAME -f | grep java | grep subsonic-booter-jar-with-dependencies.jar | awk '{print $2}' `
}
RETVAL=0
# See how we were called.
case "$1" in
start)
# Get the PID if Subsonic is running
getpid
if [ $PID ]; then
status
else
start
fi
;;
stop)
# Get the PID if Subsonic is running
getpid
if [ $PID ]; then
stop
else
status
fi
;;
status)
# Get the PID if Subsonic is running
getpid
status
;;
restart)
stop
start
;;
*)
echo "Usage: subsonic {start|stop|status|restart}"
exit 1
esac
exit $RETVAL
To setup the subsonic user and register the init-script
- Code: Select all
sudo adduser subsonic -c "Subsonic Media Streamer" -d /var/subsonic -M -s /sbin/nologin
sudo chown -R subsonic.subsonic /var/subsonic
sudo chmod og+x /etc/init.d/subsonic
sudo chkconfig --add subsonic
Example of usage
- Code: Select all
$ sudo service subsonic
Usage: subsonic {start|stop|status|restart}
$ sudo service subsonic status
Subsonic Media Streamer is not running..
$ sudo service subsonic start
Starting Subsonic Media Streamer: [ OK ]
$ sudo service subsonic status
Subsonic Media Streamer is running.. Found PID 31136
$ sudo service subsonic stop
Stopping Subsonic Media Streamer: [ OK ]
$ sudo service subsonic status
Subsonic Media Streamer is not running..