Score:0

How do I create a bash script that starts a screen session which can be detached and resumed?

eg flag

Extremely new to this (first actual script I'm writing) but I'm trying to automate some things with my minecraft server. I'm creating a bash script that will be scheduled by chron to

  1. switch the cpu governor to performance
  2. use java to start the server.jar file

Using terminal commands I've been entering screen -r java -Xmx6800M -Xms6800M -jar server.jar nogui from the /home/user/minecraft directory where I've kept all the relevant files; I had assumed that I would be able to just drop this command into the script and it would start a screen session that could be detached from. Using bash to run the script with this command starts everything like normal, but whenever I detach with Ctrl+A Ctrl+D it says "detached from" and gives a session ID for the detached screen... but then using the screen -r or -ls commands show that there are no screens to be resumed. I know that the process is still running though because I can still join my server.

Really not sure what's going on here but if there's a way to write a script that will run my server.jar file in a session that can be detached and resumed I would appreciate some advice.

bmellot2 avatar
eg flag
As an update, even though I cannot seem to find any screen sessions to reconnect to - using the pkill screen command ends the java process started by my script, so presumably there *is* a screen session somewhere? I just cannot figure out how to find and reconnect to it after detaching
user10489 avatar
in flag
This isn't really a question about bash scripts but about how to use screen. You have not included enough information to determine what your problem is. You can use ps to check if your java job is still running. Possibly you are starting screen and attempting to resume it from different users, or something is deleting screen's socket endpoints.
user535733 avatar
cn flag
So your start command works, and you can re-attach to a running session, and you have discovered the manpage that details how to list sessions. What's the question that you want an answer to? Advice: Use systemd to start/stop the server instead of a script; it's much easier.
user10489 avatar
in flag
You don't have to kill the job to find it. Try ps or pgrep -f . In Ubuntu, the screen socket directory should be in /run/screen/ and you should look there for the screen socket.
user10489 avatar
in flag
Presumably you are starting it with screen so you can interact with it later. Otherwise, I agree with @user535733 that you should not use screen for this but a systemd script.
user535733 avatar
cn flag
Well, screen is still needed for the Java server -- I've not successfully gotten the minecraft server to work without it. I've found no need to interact with the running server: All input can be done by the admin on the client, and all output is logged. What systemd does is make the starting, stopping, and checking the server status super easy, easily scriptable, and uniform with other services. Systemd's main job is to supervise services, so may as well use the correct tool for the job.
user535733 avatar
cn flag
My own start command (in the systemd `minecraft.service` file) is `ExecStart=screen -s mc -d -m java -server -Xms512M -Xmx1024M -jar server.jar nogui`
bmellot2 avatar
eg flag
Yeah I'm realizing quickly I might be going about this all wrong - initially thought crontab was the only way to schedule jobs at boot. Just came back after trying my two basic maintenance commands on reboot in crontab - the cpupower one works just fine but I still can't get the server to start properly... the command I had was ```@reboot screen -S minecraft java -Xmx6800M -Xms6800M -jar /home/filepath/server.jar nogui``` Assuming it failed maybe because directories work differently from root? Looking at using systemd now - what is the ```-server``` argument after java? That's new to me
bmellot2 avatar
eg flag
"So your start command works, and you can re-attach to a running session, and you have discovered the manpage that details how to list sessions. What's the question that you want an answer to?" The start command works but I cannot re-attach to the running screen session once I exit from it - its very strange because it even gives me the session ID number but then nothing appears using screen -r/-ls or even ps/pgrep -f
in flag
Tmux is much easier to automate than screen.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.