0

I'm hosting a small minecraft server for my friends on my home, and to make things easier for me, I wanted to make it so when i turn on the pc that is used as server, automatically starts the minecraft server. The thing is that I use tmux to be able to manage the server in case I need to. For that, I have this script:

#!/bin/bash

SESSION="server" SESSIONEXISTS=$(tmux list-sessions | grep -w "$SESSION")

if [ "$SESSIONEXISTS" = "" ] then

tmux new-session -d -s "$SESSION" -d -x "$(tput cols)" -y "$(tput lines)"

tmux rename-window -t 0 'mc' tmux send-keys -t 'mc' 'cd magma-1.18.2-40.2.10 && ./run.sh && sudo shutdown now' C-m tmux splitw -v

tmux send-keys -t 'mc' 'glances' C-m tmux select-pane -t 0 tmux splitw -h

tmux send-keys -t 'mc' 'ngrok start --all' C-m

tmux select-pane -t 0

fi

tmux attach-session -t "$SESSION":0

I've tried to use cron with no success:

@reboot bash /home/fpp/startup.sh

It just wont start the tmux session.

I also tried with a systemd unit: [Unit]

Description=mcnrelated.service

After=default.target

[Service]

ExecStart=bash /home/fpp/startup.sh

[Install]

WantedBy=default.target

but when starting up, I was able to see the service wasn't started because an error.

As a last option I tried to use rc.local:

#!/bin/bash
sudo su -c "./home/fpp/startup.sh" -s /bin/sh fpp
exit 0

but again, no tmux session was started.

Can this issue be related to tmux? or am I doing something wrong?

EDIT: The way to go is with systemd.

After chatting a lot with ChatGPT, I was able to figure out that on my script, I was using tput to get the terminal rows and columns. The issue with that is that it wont return a value when its run as a service, because its not run in a terminal, and it made the script fail. Because of that, I switched to the .tmux.conf file, so i can just call tmux -t .tmux.conf and that way I avoid using tput.

Also, as ChatGPT recommended, I'm now using a user-level systemd unit:

[Unit]
Description=Launch Minecraft Server and Related Services
After=network.target

[Service] Type=oneshot ExecStart=/usr/bin/tmux -u -f /home/fpp/tmux-config/tmux.conf RemainAfterExit=true

[Install] WantedBy=default.target

the issue now is that tmux is complaining about having no terminal to open:

fpp@fpp-server:~$ systemctl --user status mcnrelated.service
× mcnrelated.service - Launch Minecraft Server and Related Services
     Loaded: loaded (/home/fpp/.config/systemd/user/mcnrelated.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Thu 2023-09-21 11:42:09 -03; 8min ago
    Process: 1319 ExecStart=/usr/bin/tmux -u -f /home/fpp/.tmux.conf (code=exited, status=1/FAILURE)
   Main PID: 1319 (code=exited, status=1/FAILURE)
        CPU: 37ms

sep 21 11:42:09 fpp-server systemd[550]: Starting mcnrelated.service - Launch Minecraft Server and Related Services... sep 21 11:42:09 fpp-server tmux[1319]: open terminal failed: not a terminal sep 21 11:42:09 fpp-server systemd[550]: mcnrelated.service: Main process exited, code=exited, status=1/FAILURE sep 21 11:42:09 fpp-server systemd[550]: mcnrelated.service: Failed with result 'exit-code'. sep 21 11:42:09 fpp-server systemd[550]: Failed to start mcnrelated.service - Launch Minecraft Server and Related Services.

and that's where I'm now. I've tried using -u but that had no success.

The issue is something with tmux, at least for now. Once i can successfully start the service i will try to see if its auto started at boot

fpp
  • 101

0 Answers0