I am trying to create a script that opens a new tmux session and then runs a command inside that tmux session as a different user.
The purpose of this is to create a new tmux session for a game server, which has a different unix user assigned to it. I have heard that giving servers their own users was good practice, but I would still like to have a script in the home directory of my main user to start the server for convenience. I would like the tmux session to be available from my main user, yet be logged in to the game server user.
The main problem is that there is a password prompt that I cannot get around, requiring me to attach to the tmux session, enter the password, and then detach. This is what I have tried:
#!/bin/sh tmux new -d -s Minecraft sudo -u minecraft /home/minecraft/server/start.sh
I have tried every combination of running the script with sudo, adding
su - Minecraft to the script, and
su Minecraft -c "script", and in each one it either requires me to attach to the tmux session and login or just does not work, leaving no tmux session open.
I am trying to find a solution which would allow me to type in my sudo password or the other user's password when I run the script which invokes tmux, instead of having to login by attaching and then detaching.