Why is screen seemingly doing nothing with commands passed with -X?

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff8c9a8d899a8da08a8c9a8dbf8c9a8d899a8d">[email protected]</a>_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 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d7e687f7b687f52787e687f4d7e687f7b687f">[email protected]</a>_address "screen -r minecraft -X stuff 'even other_server_name is getting in on the action!
'"

Escaping ! isn’t necessary here.

Method 2

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x