7

I've been trying to set up an automated backup system for a minecraft server, and I'm having trouble with screen, specifically when using 'screen -r sessionname -X "/var/minecraft/somebatchfile"', nothing happens.

My process flow is somewhat like this at the moment:

screen -m -d -S minecraft /var/minecraft/bin/server_nogui.sh

This starts the minecraft server without any trouble. However, the issue is that even simple followups like this fail:

screen -r minecraft -X "stop"

I get no error message or success message, and the server does not actually disconnect clients and shut down, like it should. I assume I'm doing something wrong, but I don't know what. Is there some obvious mistake I'm making? I've read the man page a bit but I'm having no luck figuring it out myself.

Sukasa
  • 173

2 Answers2

7

You have to give the parameter -X a screen command, I think you want to "stuff" a minecraft-server command to the screen session.

screen -r minecraft -p 0 -X stuff "stop $(printf '\r')"

The printf send a carriage return, so the command "stop" gets executed. -p 0 makes sure the characters are sent to the initial Screen window.

For sending it over ssh you have to enclosure the command in " " (you could also use ` `, but that wouldn't let you do the command substitution).

ssh -t -i ~/.ssh/id_dsa server_user@server_address "screen -r minecraft -X stuff "even other_server_name is getting in on the action\! $(echo -ne '\r')""

Beware that ! is a reserved word, you have to escape it.

It is also possible to include a user generated newline into the command to execute it:

ssh -t -i ~/.ssh/id_dsa server_user@server_address "screen -r minecraft -X stuff 'even other_server_name is getting in on the action!
'"

Escaping ! isn't necessary here.

wag
  • 35,944
  • 12
  • 67
  • 51
  • That works great, but of course now I can't seem to run this via ssh / command line ssh -t -i ~/.ssh/id_dsa server_user@server_address screen -r minecraft -X stuff "even other_server_name is getting in on the action! $(echo -ne '\r')" I'm really not good with unix, unfortunately :/ – Sukasa Jan 14 '11 at 10:39
  • Using printf to inject the newline char is perfect. I don't know if it's an idiosyncrasy of zsh, but I could not get any of my old snippets (which used ^M as newline) to work in the current version of macOS. – stephancasas Jun 05 '22 at 17:00
1

As we discovered in this similar question, screen has issues with sending keys to sessions that have never been attached. If you have ever attached, the default window pane is set to zero, otherwise it will silently fail because the keystrokes aren't going to a window. You can avoid this by adding a -p 0 argument to your screen command.

Alternativly, you can use the much better behaved tmux instead like this:

tmux new-session -d -n minecraft /var/minecraft/bin/server_nogui.sh

Then send the minecraft server the stop command like this:

tmux send-keys -t minecraft "stop^M"

Note that the ^M sequence above is a visual representation of a real enter. You can generate this on the command like by hitting Ctrl+v Enter. This is easier than sending the carriage return using the echo statement in wag's answer.

Caleb
  • 70,105