
집에 있는 친구들을 위해 작은 마인크래프트 서버를 호스팅하고 있는데, 서버로 사용하는 PC를 켜면 자동으로 마인크래프트 서버가 시작되도록 만들고 싶었습니다. 문제는 필요할 경우 서버를 관리할 수 있도록 tmux를 사용한다는 것입니다. 이를 위해 다음 스크립트가 있습니다.
#!/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
나는 성공하지 못한 채 cron을 사용하려고 시도했습니다.
@reboot bash /home/fpp/startup.sh
tmux 세션이 시작되지 않습니다.
나는 또한 systemd 단위로 시도했습니다: [Unit]
Description=mcnrelated.service
After=default.target
[Service]
ExecStart=bash /home/fpp/startup.sh
[Install]
WantedBy=default.target
그런데 시작할 때 오류로 인해 서비스가 시작되지 않는 것을 볼 수 있었습니다.
마지막 옵션으로 rc.local을 사용하려고 했습니다.
#!/bin/bash
sudo su -c "./home/fpp/startup.sh" -s /bin/sh fpp
exit 0
하지만 다시 tmux 세션이 시작되지 않았습니다.
이 문제가 tmux와 관련이 있을 수 있습니까? 아니면 내가 뭔가 잘못하고 있는 걸까?
편집 : 갈 길은 systemd를 사용하는 것입니다.
tput
ChatGPT와 많은 대화를 나눈 후 스크립트에서 터미널 행과 열을 가져오는 데 사용하고 있다는 것을 알 수 있었습니다 . 문제는 터미널에서 실행되지 않고 스크립트가 실패하기 때문에 서비스로 실행될 때 값을 반환하지 않는다는 것입니다. 그 때문에 .tmux.conf 파일로 전환하여 호출만 tmux -t .tmux.conf
하면 tput
.
또한 ChatGPT가 권장하는 대로 이제 사용자 수준 시스템 단위를 사용하고 있습니다.
[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
이제 문제는 tmux가 열 수 있는 터미널이 없다고 불평한다는 것입니다.
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.
그리고 그것이 내가 지금 있는 곳이다. 나는 사용해 보았지만 -u
성공하지 못했습니다.
문제는 적어도 현재로서는 tmux와 관련된 것입니다. 서비스를 성공적으로 시작할 수 있으면 부팅 시 자동으로 시작되는지 확인하려고 합니다.