Universal Media Server as service/daemon

Como vimos en la entrada anterior
La forma de instalar y de configurar este servidor lógico de streaming, ahora en esta entrada vamos a ver como se puede dejar ejecutando como un daemon (si cierran la session del usuario no importa!, UMS seguirá disponible en la LAN para disponibilizar los recursos que querramos.

systemd:

Vamos a crear un usuario quien será el que ejecute el service:

$ sudo useradd -d /usr/local/bin/UMS -s /usr/sbin/nologin ums
$ sudo chown -R ums:ums /usr/local/bin/UMS

Creamos archivo de «service»: /etc/systemd/system/ums.service

[Unit]
Description=Universal Media Server

[Service]
Type=simple
Environment="UMS_PROFILE=/usr/local/bin/UMS/UMS.conf"
User=ums
Group=ums
ExecStart=/usr/local/bin/UMS/UMS.sh

[Install]
WantedBy=multi-user.target

Ahora vamos a refrescar la conf de Systemd, activar y ejectuar nuestro nuevo service:

$ sudo systemctl daemon-reload
$ sudo systemctl enable ums
$ sudo systemctl start ums

Se puede consultar a SystemD el status del service:

$ sudo systemctl status ums

SystemD captura toda la salida de la ejecución del service y lo guarda en journalctl, al ejecutarlo vamos a ver la salida que obtendríamos al ejecutar

$ journalctl

serviceStatus

sysVinit:

script para daemon:

1.- Primero debemos crear un archivo llamado UMS.sh y ubicarlo en /etc/init.d/
2.- en el contenido del archivo vamos a colocar el sig. codigo:

#!/bin/bash
#
### BEGIN INIT INFO
# Provides: ums
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts UMS program.
# Description: Java Upnp Media Server dedicated to PS3
### END INIT INFO

#set -x

#
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="Universal Media Server"
NAME=UMS.sh
CONFIG_NAME=UMS.conf
UMS_PROFILE=/usr/local/bin/UMS/$CONFIG_NAME
DAEMON=/usr/local/bin/UMS/$NAME
DAEMON_OPTS="console"
SCRIPTNAME=/etc/init.d/$NAME
UMS_START=1 # Wether to start or not UMS ver at boot time.
DODTIME=5 # Time to wait for the server to die, in seconds.
# If this value is set too low you might not
# let the program to die gracefully and 'restart' will not work

test -x $DAEMON || exit 1

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

# Include ums defaults if available
if [[ -f "/etc/default/$NAME" ]] ; then
. /etc/default/$NAME
fi

# May we run the init.d script ?
[[ $UMS_START = 1 ]] || exit 1

#--------------------------------------------------------------------------
# Some color codes
txtred=$'\e[0;31m' # Red
txtylw=$'\e[0;33m' # Yellow
txtrst=$'\e[0m' # Text Reset
#--------------------------------------------------------------------------
warnout(){
echo >&2 -e ""$txtylw"Warning:$txtrst $1"
}
#--------------------------------------------------------------------------
running(){
pid=`pgrep -f 'java .*ums.jar.*'`
}
#--------------------------------------------------------------------------
do_start(){
running && { warnout "$NAME is already running !"; exit 0; }
echo "Starting $DESC : $NAME"
UMS_PROFILE="$UMS_PROFILE" start-stop-daemon --start --quiet --background --oknodo \
--exec $DAEMON -- $DAEMON_OPTS

}
#--------------------------------------------------------------------------
do_stop(){
running || { warnout "$NAME is NOT running !"; exit 0; }
local countdown="$DODTIME"
echo -e "Stopping $DESC : $NAME \c "
kill -9 $pid
while running; do
if (($countdown >= 0)); then
sleep 1; echo -n .;
((--countdown))
else
break;
fi
done
echo
# If still running, then try to send SIGINT signal
running && { \
echo >&2 "Using kill -s SIGINT instead"; \
echo >&2 "If you see this message again, then you should increase the value of DODTIME in '$0'."; \
kill -2 $pid; \
}

if [ -e "/usr/local/bin/UMS/debug.log" ]; then
count=9
while [ $count -ge 1 ]
do
if [ -e "/usr/local/bin/UMS/debug.log.$count" ]; then
plus=$((count+1))
mv "/usr/local/bin/UMS/debug.log.$count" "/usr/local/bin/UMS/debug.log.$plus"
fi
count=$((count-1))
done
if [ -e "/usr/local/bin/UMS/debug.log" ]; then
mv "/usr/local/bin/UMS/debug.log" "/usr/local/bin/UMS/debug.log.1"
fi
fi

return 0
}
#--------------------------------------------------------------------------
do_force-stop(){
running || { warnout "$NAME is NOT running !"; exit 0; }
echo "Stopping $DESC : $NAME"
kill -9 $pid
if [ -e "/usr/local/bin/UMS/debug.log" ]; then
count=9
while [ $count -ge 1 ]
do
if [ -e "/usr/local/bin/UMS/debug.log.$count" ]; then
plus=$((count+1))
mv "/usr/local/bin/UMS/debug.log.$count" "/usr/local/bin/UMS/debug.log.$plus"
fi
count=$((count-1))
done
if [ -e "/usr/local/bin/UMS/debug.log" ]; then
mv "/usr/local/bin/UMS/debug.log" "/usr/local/bin/UMS/debug.log.1"
fi
fi
}
#--------------------------------------------------------------------------
do_status(){
echo -n " * $NAME is "
( running || { echo "NOT running "; exit 0; } )
( running && { echo "running (PID -> $(echo $pid))"; exit 0; } )
}
#--------------------------------------------------------------------------
case "$1" in

start|stop|force-stop|status)
do_${1}
;;
restart|reload)
do_stop
do_start
;;
force-restart|force-reload)
do_force-stop
do_start
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|force-stop|restart|force-restart|reload|force-reload|status}"
exit 1
;;
esac

3.- Transportamos la configuración y el archivo de log:

sudo cp -p ~/.config/UMS/UMS.conf /usr/local/bin/UMS/
sudo cp -p ~/.config/UMS/debug.log /usr/local/bin/UMS/

4.- Ya podemos lanzar el UMS como un daemon:

sudo service UMS.sh start

UMS as daemon

Pueden ver el formato de uso simplemente haciendo

sudo service UMS.sh

UMS as daemon use

Enjoy!

Un comentario sobre “Universal Media Server as service/daemon”

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Time limit is exhausted. Please reload CAPTCHA.