Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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.

share|improve this answer
    
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

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.