Skip to content

Instantly share code, notes, and snippets.

@pljones
Created October 25, 2020 21:21
Show Gist options
  • Save pljones/365c733bb284348cfa74c708497d358f to your computer and use it in GitHub Desktop.
Save pljones/365c733bb284348cfa74c708497d358f to your computer and use it in GitHub Desktop.
Jamulus service template
[Unit]
Description=Jamulus Server %i
Documentation=http://jamulus.drealm.info/jamulus
Documentation=https://github.com/corrados/jamulus/wiki/Server-Troubleshooting
After=network-online.target
Wants=network-online.target
[Install]
WantedBy=multi-user.target
[Service]
CPUSchedulingPolicy=fifo
CPUSchedulingPriority=20
IOSchedulingClass=realtime
IOSchedulingPriority=3
SyslogIdentifier=Jamulus-%i
StandardOutput=syslog
StandardError=syslog
User=Jamulus
Group=Jamulus
WorkingDirectory=~
Environment=UNIT=%i
EnvironmentFile=/opt/Jamulus/systemd/Jamulus.env
EnvironmentFile=/opt/Jamulus/systemd/%i.env
ExecStart=/opt/Jamulus/bin/run-Jamulus-template.sh
@pljones
Copy link
Author

pljones commented Oct 25, 2020

Note that I use symlinks a lot. So the above is

lrwxrwxrwx 1 root root 45 Aug  2 13:28 /etc/systemd/system/Jamulus@.service -> ../../../opt/Jamulus/systemd/Jamulus@.service

and this Jamulus service template - service-specific override, Jamulus@.service.d/Jamulus-server.conf is actually

lrwxrwxrwx 1 root root 53 Aug  2 15:01 /etc/systemd/system/Jamulus@server.service.d -> ../../../opt/Jamulus/systemd/Jamulus@server.service.d

(And, in fact, even the files in /opt/Jamulus are mostly symlinks elsewhere to give me files in one place under git control - but not on github.)

[Unit]
Description=Jamulus at drealm.info

[Service]
TimeoutStopSec=2

You only need the above if you've got something that needs to change per instance, other than what's configured in the environment files -- i.e. specifically to do with systemd itself.

So those environment files:

EnvironmentFile=/opt/Jamulus/systemd/Jamulus.env

is this:

# Where everything is
JAMULUS=Jamulus
JAMULUS_ROOT=/opt/Jamulus
JAMULUS_BINDIR=/opt/Jamulus/bin

# Actual defaults
JAMULUS_FASTUPDATE=true
JAMULUS_MULTITHREADED=false
JAMULUS_CENTRAL=
JAMULUS_PORT=
JAMULUS_MAXCHANS=
JAMULUS_STATUSPAGE=
JAMULUS_LOGFILE=
JAMULUS_ENABLE_RECORDING=false
JAMULUS_RECORDING_DIR=
JAMULUS_SERVERNAME=
JAMULUS_SERVERINFO=
JAMULUS_WELCOMEMSG=

One of the

EnvironmentFile=/opt/Jamulus/systemd/%i.env

files -- %i is the instance name (so when you systemctl start Jamulus@instance, %i would be instance) might look like:

JAMULUS_MULTITHREADED=true
JAMULUS_CENTRAL=jamulusallgenres.fischvolk.de:22224
JAMULUS_PORT=54850
JAMULUS_MAXCHANS=50
JAMULUS_STATUSPAGE=/opt/Jamulus/run/status.html
JAMULUS_LOGFILE=/opt/Jamulus/log/Jamulus.log
JAMULUS_ENABLE_RECORDING=true
JAMULUS_RECORDING_DIR=/opt/Jamulus/run/recording
JAMULUS_SERVERNAME=jamulus.drealm.info
JAMULUS_SERVERINFO="jamulus.drealm.info;London;224"
JAMULUS_WELCOMEMSG=/opt/Jamulus/systemd/welcome-server.html

And the final bit of "magic" is the run script, which simply maps the env variables to the command line:

ExecStart=/opt/Jamulus/bin/run-Jamulus-template.sh

The main point to note is DAEMON="$JAMULUS_BINDIR/${JAMULUS}-${UNIT}", where that Environment=UNIT=%i comes in handy. It specifies the binary to execute based on the instance name. I have "stable", "current" and "work in progress" versions of Jamulus server, symlinked from the DAEMON name.

#!/bin/bash -e
export LANG=C PATH="/usr/bin:/bin:$JAMULUS_BINDIR"
DAEMON="$JAMULUS_BINDIR/${JAMULUS}-${UNIT}"
if [ ! -x "$DAEMON" ] ; then
        echo "$DAEMON not installed." >&2
        exit 1
fi

JAMULUS_OPTS=("-s" "-n")
if [ ! -z "$JAMULUS_FASTUPDATE" ] && $JAMULUS_FASTUPDATE; then JAMULUS_OPTS+=("-F"); fi
if [ ! -z "$JAMULUS_MULTITHREADED" ] && $JAMULUS_MULTITHREADED; then JAMULUS_OPTS+=("-T"); fi

if [ ! -z "$JAMULUS_PORT" ] ; then JAMULUS_OPTS+=("-p" $JAMULUS_PORT); fi
if [ ! -z "$JAMULUS_MAXCHANS" ] ; then JAMULUS_OPTS+=("-u" $JAMULUS_MAXCHANS); fi
if [ ! -z "$JAMULUS_CENTRAL" ] ; then JAMULUS_OPTS+=("-e" "$JAMULUS_CENTRAL"); fi

if [ ! -z "$JAMULUS_STATUSPAGE" -a -z "$JAMULUS_SERVERNAME" ] ; then
        echo "if STATUSPAGE set, SERVERNAME must be set" >&2
        exit 1
fi
if [ ! -z "$JAMULUS_STATUSPAGE" ] ; then JAMULUS_OPTS+=("-m" "$JAMULUS_STATUSPAGE"); fi
if [ ! -z "$JAMULUS_LOGFILE" ] ; then JAMULUS_OPTS+=("-l" "$JAMULUS_LOGFILE"); fi

if [ ! -z "$JAMULUS_ENABLE_RECORDING" ] && $JAMULUS_ENABLE_RECORDING && [ ! -z "$JAMULUS_RECORDING_DIR" ]
then
        JAMULUS_OPTS+=("-L" "-R" "$JAMULUS_RECORDING_DIR")
fi

if [ ! -z "$JAMULUS_SERVERINFO" ] ; then JAMULUS_OPTS+=("-o" "$JAMULUS_SERVERINFO"); fi
if [ ! -z "$JAMULUS_WELCOMEMSG" ] ; then JAMULUS_OPTS+=("-w" "$JAMULUS_WELCOMEMSG"); fi

echo "Starting Jamulus server" ;#"$JAMULUS_SERVERNAME"
if [ ! -z "$JAMULUS_STATUSPAGE" -a -e "$JAMULUS_STATUSPAGE" ] ; then
        cat > "$JAMULUS_STATUSPAGE" << !EOF
$JAMULUS_SERVERNAME Jamulus server is starting.
!EOF
fi

echo exec ${DAEMON} "${JAMULUS_OPTS[@]}"
exec ${DAEMON} "${JAMULUS_OPTS[@]}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment