如何在 Ubuntu 中自动启动 tmux bash 脚本作为启动服务?

问题描述 投票:0回答:1

我曾经在 Raspberry Pi 4 上运行 TShock 服务器。我最近获得了更强大的硬件,即带有新安装的 Ubuntu 23.10 的 Intel“Skull Canyon”NUC6i7KYK,并且我正在尝试使用

 复制我的服务器设置systemd
而不是像我在 Pi 上那样使用
init.d
rc.local

我正在寻找在系统启动期间在

tmux
内部启动服务器的最佳方法,因为我当前将其作为通过服务运行的bash脚本启动的策略可能会使事情变得过于复杂。这是我目前的方法:

我这里有一个运行服务器的 shell 脚本:

/home/terraria/start-server.sh

#!/bin/bash
/home/kokonoe/terraria/tshock/TShock.Server -config /home/kokonoe/terraria/serverconfig

另一个 shell 脚本,在

tmux
会话中启动服务器,使用
htop
进行分屏:

/home/terraria/autostart.sh

#!/bin/bash
export DOTNET_ROOT=/home/kokonoe/terraria/tshock/dotnet  # this is normally set in ~/.bashrc, but systemctl doesn't seem to see it there.
tmux new -s tshock -d "/bin/bash /home/kokonoe/terraria/start-server.sh"
tmux split-window -h "htop"
tmux last-pane

这两个脚本在终端会话中运行良好,并将按预期启动服务器。但是,我正在努力编写一个

systemctl
服务来在启动时启动服务器。我这里有一个 .system 文件:

/etc/systemd/system/terraria-autostart.service

[Unit]
Description=Runs TShock in a tmux session.

[Service]
ExecStart=/bin/bash /home/kokonoe/terraria/autostart.sh
User=kokonoe
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

我跑了

sudo systemctl daemon-reload
,然后
sudo systemctl start terraria-autostart
。我期望发生的是创建一个
tmux
会话,水平分割,
tshock
在左侧,
htop
在右侧,就像我运行
~/terraria/autostart.sh
时发生的情况一样,但相反,我只看到
htop
而没有其他内容,因此服务器脚本退出并关闭其窗格。

我怀疑环境变量或

PATH
之类的东西有些奇怪,但我不确定从哪里开始,或者服务是否是实现启动脚本的最佳方式。感谢您的关注!

bash tmux systemctl ubuntu-23.10
1个回答
0
投票

经过一些修改设置环境变量(例如采购

.profile
.bashrc
)后,通过服务运行它并没有成功。然而,通过
cron
作为
@reboot
作业运行脚本最终按预期工作。我不知道两者在环境方面有何不同,但这也许可以帮助未来的游戏玩家!

© www.soinside.com 2019 - 2024. All rights reserved.