Linux script to utilize GNU screen.

Bop

2[H]4U
Joined
Oct 1, 2003
Messages
3,307
I am somewhat new to UNIX/Linux type systems and I am looking to write a script for my Minecraft server.

Basically my server will be connected to a UPS and a script will be executed when the server is on battery power.

Minecraft is started with this script(leaving the memory values for java out):

screen -mS mcraft java -jar craftbukkit.jar

I want a new script to execute the "stop" command to the one screen session that is running. This screen is not running as the root user. Let's say for the sake of example the user is "game." Since the UPS script will be executed as root and the other needs to be executed as game, I am not sure how I would pipe into the proper screen session correctly.

Essentially it breaks down to:
UPS shutdown script reaches a line where it runs my custom script.
Log in as user "game"
Resume screen session or pipe command directly to specific screen session "mcraft".
Issue stop command
Other script resumes to shut down the system(would I have to switch back to root explicitly?)


EDIT: Figured part of it out. The game admin can run a script that executes this command:$ screen -x mcraft -X stuff "stop^M" to stop the MC server. However, if I share the screen session and run screen -x game/mcraft -X stuff "stop^M" as root it doesn't work. I can view the share session as root, but not send commands to it.

2ND EDIT: Figured it all out. I had to make a screen within a screen so the root script could send commands to the game admin's screen. Very odd way to do it, but for now it will do. Here is the script in case anyone ever needs something like it:
Code:
#!/bin/bash
screen -dmS shutdown -t one screen -x game/
screen -x shutdown -p one -X stuff "stop^M"
 
Last edited:
Back
Top