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
  • 1
  • 2
  • "before login as user", "(not root)" and "After=default.target" probably will contradict with each other ... Which one do you want? – Raffa Sep 21 '23 at 11:05
  • @Raffa I want the script to be executed as user, as soon as the system has completed the boot process. I don't really know what I'm doing honestly, i just followed this guide and i had no luck on what i want to do – fpp Sep 21 '23 at 12:55
  • When using a system service, you’ll need to specify the full real path to interpreter e.g. /bin/bash and then your script will run as root by default unless you explicitly specify a user and your user's crontab won’t work before login that way as you seem to use commands in your script that require a user graphical session running AFAIK and even after login a user service is preferred to crontab for such script or you might be able to run your script as a startup application after login – Raffa Sep 21 '23 at 13:31
  • @Raffa thanks for the suggestions. I think i'm getting closer and closer. It seems that when using systyemd (as root or as user) it was complaining about an "incorrect size". That had to do with tput command, that isnt supposed to work on a non-tty environment. So i changed routes and decided to make a .tmux.conf file so i dont rely on tput. The issue now is that tmux is complaining about not having a terminal available, and i cant seem to find a solution. I updated the question so there is more context – fpp Sep 21 '23 at 14:52
  • I was able to solve my issue by following this solution. – fpp Sep 22 '23 at 23:12

0 Answers0