After recently switching to running Sage on a linux server after a couple years on Vista, I thought I'd share the following initscripts I've written for use with my OpenSuSE 11.2 x86_64 installation. The caveats are that they probably aren't suitable for someone who isn't at least comfortable with bash shell scripting and system administration, and I probably won't be able to give much support. There's no sanity-checking, either.
First, the sagetvserver initscript. In addition to providing a unified way to start/stop the SageTV daemon, it also provides for 1) running as a non-root user, and 2) specifying an alternate Java path (necessary if your main java is 64-bit). The main script is /etc/init.d/sagetvserver, settings in /etc/sysconfig/sagetvserver. An advantage of this script is that it doesn't require modifying any of the sagetv-supplied shell scripts.
If you want to run as a user other than root, you will have to first create the sagetv user, and make sure it has access to the sagetv install directory and to the video devices which it will use (perhaps by adding it to the video group). I'd also suggest disabling logins for the sagetv user and setting its login shell to /bin/false.
Code:
useradd --system -d <your sagetv directory> -s /bin/false -g video -c "SageTV user" <sagetv, or your sagetv username>
chown -R sagetv.video <your sagetv directory>
Note that you should only use a pre-existing group like "video" for sage if no other users have that membership, or you don't mind those users being able to do everything that Sage can.
/etc/init.d/sagetvserver
Code:
#! /bin/sh
### BEGIN INIT INFO
# Provides: sagetvserver
# Required-Start: $network $remote_fs dbus
# Required-Stop: $network $remote_fs dbus
# Default-Start: 3 5
# Default-Stop:
# Short-Description: Launches SageTV server
# Description: Launches SageTV server
### END INIT INFO
. /etc/rc.status
rc_reset
[[ -x $JAVA_HOME/bin/java ]] && SAGEJAVADIR=$JAVA_HOME
[[ -x /opt/sagetv/server/startsage ]] && SAGEPATH=/opt/sagetv/server
SAGEUSER=sagetv
. /etc/sysconfig/sagetvserver
case "$1" in
start)
echo -n "Starting SageTV server "
touch /var/run/sagetv.pid && chown $SAGEUSER /var/run/sagetv.pid
sudo -u $SAGEUSER PATH=$SAGEJAVADIR:$PATH $SAGEPATH/startsage 2> /dev/null
rc_status -v
;;
stop)
echo -n "Shutting down SageTV "
$SAGEPATH/stopsage
rc_status -v
;;
restart)
$0 stop
$0 start
rc_status
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
/etc/sysconfig/sagetvserver
Code:
#Enter the name of the user Sage should run as. Make sure the user has
#been created and has rights to the sagetv directory and the video devices
#Sage will use (perhaps by adding that user to the video group).
SAGEUSER=sagetv
#Replace with the directory where your 32-bit java executable is
SAGEJAVADIR=/opt/sagetv/java/jre1.6.0/bin
#Replace with the directory where the SageTV server is installed
SAGEPATH=/opt/sagetv/server
This is the initscript I've been using for the SJQ client. As it is very late I will leave configuration as an exercise for the reader. It also can run as a non-root user (if running on the same host as the SageTV server, the user most likely will be the one configured for sagetv above). Unlike the sagetvserver script above, this one sudoes the executable directly, rather than by calling the sjqc.sh script supplied with the client package, and may need to be modified if the java commandline used to launch sjqc changes.
/etc/init.d/sjqc
Code:
#! /bin/sh
### BEGIN INIT INFO
# Provides: SageTV Job Queue Client Daemon
# Required-Start: $network $remote_fs dbus sagetvserver
# Required-Stop: $network $remote_fs dbus sagetvserver
# Default-Start: 3 5
# Default-Stop:
# Short-Description: Launches SJQ Client Daemon
# Description: Launches SJQ Client Daemon
### END INIT INFO
. /etc/rc.status
rc_reset
[[ -x $JAVA_HOME/bin/java ]] && SJQJAVA=`dirname $JAVA_HOME`
SAGEUSER=sagetv
SJQBASE=/opt/sagetv/sjqc
SJQCCONFIGDIR=/etc/sjqc
. /etc/sysconfig/sjqc
JAVA=$SJQJAVA/bin/java
case "$1" in
start)
echo -n "Starting SJQ Client Daemon "
if [[ ! -x $JAVA ]]; then
echo "\$JAVA_HOME ($JAVA) does not appear to point to a valid JRE"
exit 1
fi
sudo -u $SAGEUSER $JAVA -cp $SJQBASE/json.jar:$SJQBASE/sjqc.jar com.google.code.sagetvaddons.sjqc.TaskClient "$SJQCONFIGDIR" <&- &
sjqpid=$!
echo "Process started with PID $sjqpid"
echo $sjqpid > /var/run/sjqc.pid
# SJQC takes a few seconds to print out it's startup info
sleep 3
rc_status -v
;;
stop)
if [ -e /var/run/sjqc.pid ] ; then
sjqpid=`cat /var/run/sjqc.pid`
running=`ps --pid $sjqpid | grep java`
fi
if [ "$running" = "" ]; then
echo SageTV not currently running
exit 1
fi
kill $sjqpid
rc_status -v
;;
restart)
$0 stop
$0 start
rc_status
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
/etc/sysconfig/sjqc
Code:
# Replace with the effective user for SageTV, or a dedicated user
SAGEUSER=sagetv
#Replace with the directory where your 32-bit java executable resides
SJQJAVA=/opt/sagetv/java/jre1.6.0/bin
# The SJQ properties directory (/etc/sjqc is the default)
SJQCONFIGDIR=/etc/sjqc
# The directory where the SJQ client was installed
SJQBASE=/opt/sagetv/sjqc
Please post any corrections. Hope some people find this useful.