我尝试在 ubuntu 16.04 服务器上为 red5-server 构建并安装 systemd 脚本。
我在以下位置找到了示例 init.d 脚本https://gist.github.com/akarambir/a40163f163ae8b131be8并下载
我将脚本命名为red5.sh
并添加到/etc/init.d
文件模式 755,并修改了red5.sh
文件安装位置的脚本路径
#!/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
然后我创建 red5.service 文件并将其添加到 /etc/systemd/system 文件夹,文件模式为 755。然后我运行systemctl reload-daemon
并systemctl enable red5.service
[Unit]
Description=Red5
[Service]
Type=simple
ExecStart=/etc/init.d/red5.sh
[Install]
WantedBy=multi-user.target
无论如何,当我尝试运行服务systemctl start red5.service
并使用检查状态时systemctl status red5.service
,我收到此错误:
● 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'.
但是当我尝试使用sh /etc/init.d/red5.sh start
commend 运行 init.d 脚本时,它已成功启动。
我遗漏了什么吗?
答案1
@MichaelHampton 说得对,你的 systemd 单元不应该调用 SysVinit 脚本,它应该只调用启动服务器的常规可执行文件。可能是这样的:
ExecStart=/root/red5-server/red5.sh
systemd
可以处理 PID 文件管理并记录到 STDOUT,因此很多旧的 SysVinit 脚本都不再需要。
但是,坚持错误的模式,存在一个简单的问题,您提供的错误输出中暗示了这一点:
Usage: /etc/init.d/red5.sh {start|stop|restart|force-reload}
systemd 单元应该调用/etc/init.d/red5.sh start
not /etc/init.d/red5.sh
。但是,这种解决方案也应避免——它增加了显著的额外复杂性和复杂性,可能会导致以后甚至更早出现问题。