1

I tried to build and install systemd script for red5-server on ubuntu 16.04 server.

I found the sample init.d script at https://gist.github.com/akarambir/a40163f163ae8b131be8 and downloaded

I named the script with red5.sh and added to /etc/init.d with file mode 755 and modified the path of the red5.sh script path for where the file is installed

#!/bin/sh -
#
# red5      red5 server initscript
#
# Author:   Karambir Singh Nain <akarambir@gmail.com>
#
### BEGIN INIT INFO
# Provides:     red5
# Required-Start:   $remote_fs $syslog
# Required-Stop:    $remote_fs $syslog
# Default-Start:    2 3 4 5
# Default-Stop:     
# Short-Description:    red5 media server
### END INIT INFO

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="Red5 flash streaming server"
NAME=red5
#RED5_HOME=/usr/local/red5
RED5_HOME=/root/red5-server
DAEMON=$RED5_HOME/$NAME.sh
PIDFILE=/var/run/$NAME.pid
LOGFILE=/var/log/$NAME.log
SCRIPTNAME=/etc/init.d/$NAME.sh

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

# Read config file if it is present.
if [ -r /etc/default/$NAME ]
then
    . /etc/default/$NAME
fi

#
#   Function that starts the daemon/service.
#
d_start() {
    start-stop-daemon --start --pidfile $PIDFILE \
                --chdir $RED5_HOME --background --make-pidfile \
        --startas /bin/bash -- -c "exec $DAEMON > $LOGFILE 2>&1"
}

#
#   Function that stops the daemon/service.
#
d_stop() {
    start-stop-daemon --stop --quiet --pidfile $PIDFILE \
        --name java
        rm -f $PIDFILE 
}

case "$1" in
  start)
    echo -n "Starting $DESC: $NAME"
    d_start
    echo "."
    ;;
  stop)
    echo -n "Stopping $DESC: $NAME"
    d_stop
    echo "."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: $NAME"
    d_stop
    sleep 1
    d_start
    echo "."
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0

And then I create red5.service file and added to /etc/systemd/system folder with file mode 755. Then I run systemctl reload-daemon and systemctl enable red5.service

[Unit]
Description=Red5

[Service]
Type=simple
ExecStart=/etc/init.d/red5.sh

[Install]
WantedBy=multi-user.target

Anyway, when I try to run service with systemctl start red5.service and check the status with systemctl status red5.service, I receive this error:

● red5.service - Red5
   Loaded: loaded (/etc/systemd/system/red5.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2017-02-01 14:36:07 CET; 4s ago
  Process: 5236 ExecStart=/etc/init.d/red5.sh (code=exited, status=1/FAILURE)
 Main PID: 5236 (code=exited, status=1/FAILURE)

Feb 01 14:36:07 Ubuntu-1604-xenial-64-minimal systemd[1]: Started Red5.
Feb 01 14:36:07 Ubuntu-1604-xenial-64-minimal red5.sh[5236]: Usage: /etc/init.d/red5.sh {start|stop|restart|force-reload}
Feb 01 14:36:07 Ubuntu-1604-xenial-64-minimal systemd[1]: red5.service: Main process exited, code=exited, status=1/FAILURE
Feb 01 14:36:07 Ubuntu-1604-xenial-64-minimal systemd[1]: red5.service: Unit entered failed state.
Feb 01 14:36:07 Ubuntu-1604-xenial-64-minimal systemd[1]: red5.service: Failed with result 'exit-code'.

But when I try to run init.d script with sh /etc/init.d/red5.sh start commend, it's successfully started.

Am I missing something??

Sencer H.
  • 532
  • 1
  • 8
  • 17
  • That's not really making a proper systemd unit. It's just trying to call the old-style init script. But systemd does that properly already without any help. Worse, this anti-pattern is in the [official github repo](https://github.com/Red5/red5-service/tree/master/src/main/daemon)! Best to ask the developers to please create a proper systemd unit. – Michael Hampton Feb 01 '17 at 17:11

1 Answers1

1

@MichaelHampton is right that your systemd unit shouldn't be calling a SysVinit script, it should just be calling a regular executable that starts the server. Maybe something like this:

ExecStart=/root/red5-server/red5.sh

systemd can handle managing the PID files and logging to STDOUT, so lot of what the old SysVinit script is not necessary.

But, sticking with the bad pattern, there is a simple problem which is hinted at in the error output you provided:

Usage: /etc/init.d/red5.sh {start|stop|restart|force-reload}

The systemd unit should be calling /etc/init.d/red5.sh start not /etc/init.d/red5.sh. However, this solution is too be avoided-- it adds significant extra complexity and complication that is likely to lead to problems later, if not sooner.

Mark Stosberg
  • 3,771
  • 23
  • 27
  • 1
    I considered to append `start` to end of `ExecStart` line, once. But it's not solved the problem. I will try to run service via altering the `ExecStart` line to `ExecStart=/root/red5-server/red5.sh` systemd script and make avoid init.d script. – Sencer H. Feb 02 '17 at 07:52
  • Let me know it goes. If it works out, please upvote and accept my answer. – Mark Stosberg Feb 02 '17 at 19:09
  • Unfortunately it did not worked. Still giving same error code `ExecStart=/root/red5-server/red5.sh (code=exited, status=1/FAILURE)` with different error `Error: Could not find or load main class org.red5.server.Bootstrap` And I have no idea why it's giving class error. Looks like problem escelated to anther level. – Sencer H. Feb 03 '17 at 08:56
  • There could be another environment variable that needs to be set, but it's not clear what. You could try setting the same PATH in systemd that was set by init.d: `Environment="PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"` – Mark Stosberg Feb 03 '17 at 18:17
  • You could try this on the CLI and see what files are attempted to accessed. That could provide a clue: `strace /root/red5-server/red5.sh`. It just spews out all the system calls being made. Look for file paths in the output to follow the flow of what's happening. – Mark Stosberg Feb 03 '17 at 18:18