用于在 chroot 中运行守护进程的 SystemD 服务

用于在 chroot 中运行守护进程的 SystemD 服务

我已经设置了运行 ubuntu 20.04 服务器的 Raspberry Pi,以便它可以与 box86 运行 teamspeak。

为此,我ls /srv/chroot/focal-armhf/使用 debootstrap 设置了一个 chroot。在该目录中,我编译了 box86。然后,我在 linux 上下载并安装了 x86 版 ts3server。我为 schroot 设置了一个自定义配置。

/etc/schroot/chroot.d/focal-armhf.conf

[focal-armhf]
description=Ubuntu focal armhf chroot
type=directory
directory=/srv/chroot/focal-armhf
profile=ts3server
personality=linux
# preserve-environment=true
root-users=ubuntu
users=ubuntu

当我在主机上的 bash 中运行以下命令时,一切正常。

# beginn session, name it ts3server
sudo /usr/bin/schroot -n ts3server -b -c chroot:focal-armhf

# run start script. it's the same as the default start script, however I added /usr/local/bin/box86 in front of the starting of the actual server binary.
sudo /usr/bin/schroot --run-session --chroot ts3server -d /opt/ts3server/teamspeak3-server_linux_x86/ './ts3custom.sh' start

# at this point the server is running and i can connect to it.
# ...

# when I want to shut down:
# stopping server
/usr/bin/schroot --run-session --chroot ts3server -d /opt/ts3server/teamspeak3-server_linux_x86/ './ts3custom.sh' stop

# ending schroot session
/usr/bin/schroot -e --chroot ts3server

我现在想用 systemd 自动执行此操作。所以我创建了这个单元配置

[Unit]
Description=Teamspeak3 Server in chroot
After=network.target
After=syslog.target

[Service]
ExecStartPre=/usr/bin/schroot -n ts3server -b -c chroot:focal-armhf
ExecStart=/usr/bin/schroot --run-session --chroot ts3server -d /opt/ts3server/teamspeak3-server_linux_x86/ './ts3custom.sh' start
ExecStop=/usr/bin/schroot --run-session --chroot ts3server -d /opt/ts3server/teamspeak3-server_linux_x86/ './ts3custom.sh' stop
ExecStopPost=/usr/bin/schroot -e --chroot ts3server
Restart=no
Type=simple

[Install]
WantedBy=multi-user.target

但是当我运行它时,它会立即关闭。teamspeak 服务器日志运行是

2021-05-06 17:58:49.429511|INFO    |ServerLibPriv |   |TeamSpeak 3 Server 3.13.3 (2020-12-16 14:17:05)
2021-05-06 17:58:49.456286|INFO    |ServerLibPriv |   |SystemInformation: Linux 5.4.0-1034-raspi #37-Ubuntu SMP PREEMPT Mon Apr 12 23:14:49 UTC 2021 i686 Binary: 32bit
2021-05-06 17:58:49.456975|WARNING |ServerLibPriv |   |The system locale is set to "C" this can cause unexpected behavior. We advice you to repair your locale!
2021-05-06 17:58:49.508403|INFO    |DatabaseQuery |   |dbPlugin name:    SQLite3 plugin, Version 3, (c)TeamSpeak Systems GmbH
2021-05-06 17:58:49.509804|INFO    |DatabaseQuery |   |dbPlugin version: 3.11.1
2021-05-06 17:58:49.553828|INFO    |DatabaseQuery |   |checking database integrity (may take a while)

如果我设置始终重启,它将不断生成相同的日志。
好像服务器启动后,然后分叉到后台,然后 systemd 调用 ExecStop。

我尝试设置Type=forkingType=execType=simple,结果都相同。

基本上一切都运行良好,直到我尝试使用 systemd 来管理这一切。

我怎样才能让它工作?

我特别想要:

  • 随系统自动启动
  • 管理它service ts3server start|stop|restart

为了完整起见,我使用自定义脚本来管理 Teamspeak 服务器:

#!/bin/sh
# Copyright (c) 2019 TeamSpeak Systems GmbH
# All rights reserved

BINARYNAME="./ts3server"
COMMANDLINE_PARAMETERS="" #add any command line parameters you want to pass here
PID_FILE=ts3server.pid

do_start() {
        if [ -e $PID_FILE ]; then
                PID=$(cat "$PID_FILE")
                if (ps -p "$PID" >/dev/null 2>&1); then
                        echo "The server is already running, try restart or stop"
                        return 1
                else
                        echo "$PID_FILE found, but no server running. Possibly your previously started server crashed"
                        echo "Please view the logfile for details."
                        rm $PID_FILE
                fi
        fi
       if [ $(id -u) -eq 0 ]; then
               echo "WARNING ! For security reasons we advise: DO NOT RUN THE SERVER AS ROOT"
               c=1
               while [ "$c" -le 10 ]; do
                       echo -n "!"
                       sleep 1
                       c=$(($c + 1))
               done
               echo "!"
       fi
        echo "Starting the TeamSpeak 3 server"
        if [ ! -e "$BINARYNAME" ]; then
                echo "Could not find binary, aborting"
                return 5
        fi
        if [ ! -x "$BINARYNAME" ]; then
                echo "${BINARYNAME} is not executable, trying to set it"
                chmod u+x "${BINARYNAME}"
        fi
        if [ ! -x "$BINARYNAME" ]; then
                echo "${BINARNAME} is not exectuable, cannot start TeamSpeak 3 server"
                return 3
        fi
        /usr/local/bin/box86 "./${BINARYNAME}" "${@}" "daemon=1" "pid_file=$PID_FILE"
        if [ $? -eq 0 ]; then
                echo "TeamSpeak 3 server started, for details please view the log file"
        else
                echo "TeamSpeak 3 server could not start"
                return 4
        fi
}

do_stop() {
        if [ ! -e $PID_FILE ]; then
                echo "No server running ($PID_FILE is missing)"
                return 0
        fi
        PID=$(cat "$PID_FILE")
        if (! ps -p "$PID" >/dev/null 2>&1); then
                echo "No server running"
                return 0
        fi

        echo -n "Stopping the TeamSpeak 3 server "
        kill -TERM "$PID" || exit $?
        rm -f $PID_FILE
        c=300
        while [ "$c" -gt 0 ]; do
                if (kill -0 "$PID" 2>/dev/null); then
                        echo -n "."
                        sleep 1
                else
                        break
                fi
                c=$(($c - 1))
        done
        echo
        if [ $c -eq 0 ]; then
                echo "Server is not shutting down cleanly - killing"
                kill -KILL "$PID"
                return $?
        else
                echo "done"
        fi
        return 0
}

do_status() {
        if [ ! -e $PID_FILE ]; then
                echo "No server running ($PID_FILE is missing)"
                return 1
        fi
        PID=$(cat "$PID_FILE")
        if (! ps -p "$PID" >/dev/null 2>&1); then
                echo "Server seems to have died"
                return 1
        fi
        echo "Server is running"
        return 0
}

# change directory to the scripts location
cd $(dirname $([ -x "$(command -v realpath)" ] && realpath "$0" || readlink -f "$0"))

case "$1" in
start)
        shift
        do_start "$@" "$COMMANDLINE_PARAMETERS"
        exit $?
        ;;
stop)
        do_stop
        exit $?
        ;;
restart)
        shift
        do_stop && (do_start "$@" "$COMMANDLINE_PARAMETERS")
        exit $?
        ;;
status)
        do_status
        exit $?
        ;;
*)
        echo "Usage: ${0} {start|stop|restart|status}"
        exit 2
        ;;
esac

答案1

我在使用 tmux 时遇到过类似的问题,解决方法如下:

[Install]
WantedBy=multi-user.target

[Service]
Type=oneshot
RemainAfterExit=yes
KillMode=none
User=root
ExecStart=...
WorkingDirectory=

看起来该服务已结束,因为其父服务(服务)已停止。RemainAfterExit 和 KillMode 有所帮助...

相关内容