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 <[email protected]>
#
### 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??
@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:
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:
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.