首页 > 解决方案 > 如何从系统服务运行 bash 脚本?

问题描述

所以我试图从一个 bash 脚本启动一个 tmux 屏幕,然后它将托管一个(python)discordbot。反过来,该 bash 脚本应该从系统服务启动,以便 discordbot 始终与物理服务器本身一起启动。似乎由于某种原因它卡在了某个地方……我一直在寻找解决方案,但一直找不到。

这是系统服务输出:

● ubuntubot.service - UbuntuBot
     Loaded: loaded (/etc/systemd/system/ubuntubot.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2021-08-13 18:39:26 CEST; 1min 58s ago
   Main PID: 27366 (tmux: server)
      Tasks: 2 (limit: 19060)
     Memory: 4.9M
     CGroup: /system.slice/ubuntubot.service
             ├─27366 /usr/bin/tmux new-session -d -s ubuntubot
             └─27367 -bash

这是我的服务文件:

[Unit]
Description=UbuntuBot
After=network.target

[Install]
WantedBy=multi-user.target

[Service]
Type=simple
ExecStart=/usr/bin/bash /home/flynn/Minecraft/CreativeServer/DiscordBot/launchbot.sh start
Restart=always
RestartSec=5

这是我的 bash 脚本:

#!/bin/bash
#This script launches the Ubuntubot in a different tmux screen

#Start tmux ubuntu-session:
/usr/bin/tmux new-session -d -s ubuntubot

#Start UbuntuBot
/usr/bin/tmux send-keys -t ubuntubot "python3 ~/Minecraft/CreativeServer/DiscordBot/Ubuntubot.py" Enter

也许这是一种可怕的方法,我真的不知道......我只需要一些帮助,因为这让我自己很长时间:)

标签: pythonlinuxbashservicetmux

解决方案


推荐阅读