首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >chroot中运行deamon的SystemD服务

chroot中运行deamon的SystemD服务
EN

Ask Ubuntu用户
提问于 2021-05-06 18:10:43
回答 1查看 2.5K关注 0票数 1

我已经设置了运行ubuntu20.04服务器的raspberry pi,这样它就可以运行box86。

要做到这一点,我在ls /srv/chroot/focal-armhf/中设置了一个chroot和debootstrap。在那个目录中,我编译了box86。然后,我在linux上下载并安装了ts3server for x86。我已经为schroot设置了自定义配置。

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

代码语言:javascript
复制
[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运行以下命令时,一切正常。

代码语言:javascript
复制
# 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自动完成这个任务。所以我创建了这个单元配置

代码语言:javascript
复制
[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

但是,当我运行它时,它会立即关闭。工作组语言服务器日志运行的内容是

代码语言:javascript
复制
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管理它

为了完整起见,我的自定义脚本可以管理“团队语言”服务器:

代码语言:javascript
复制
#!/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
EN

回答 1

Ask Ubuntu用户

回答已采纳

发布于 2021-05-06 19:01:37

我在tmux上也遇到过类似的问题,就这样解决了它:

代码语言:javascript
复制
[Install]
WantedBy=multi-user.target

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

自其父服务(服务)停止以来,该服务看起来已经结束了。RemainAfterExit和KillMode帮忙..。

票数 2
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/1336570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档