0

So, I host multiple Minecraft servers on my Ubuntu VM and I've setup a script to start all of them at once. The servers are running in screen sessions and basically what the script does is send the java start command to these screen sessions and if a screen session can't be found creates one. The problem is the screen sessions default to your current working directory and the jars I'm targeting are all in different directories (I.E not my current directory) So, so for example after a reboot, when all the screen sessions are offline and need to be started, they aren't started in the correct directory. So, what I need is these new screen sessions that are created to start in a specific directory or else a way to target the jars from a different directory and set their working directory. I've been trying for a while to find a solution for this but haven't found anything. Here are my scripts:

This is just one of the scripts I use to start my server, but the other scripts are mostly the same just different starting flags. I removed the memory flags as they weren't relevant (to my knowledge) and just made the code block really long.:

#!/bin/bash
if
screen -S "SMP" -X stuff 'java [Lots of fine-tuned java memory flags] -jar paper.jar nogui^M'
then
echo "Server starting on prexisting screen"
else
echo "Cannot find prexsisting screen, attempting to create a new one."
screen -dmS "SMP"
screen -S "SMP" -X stuff 'java [Lots of fine-tuned java memory flags] -jar paper.jar nogui^M'
echo "Loaded server on a new screen"
fi

Here is the script I call the above one from. I wrote all the start scripts in their own .sh files and then just call them all from one file (This is the script that I actually use to start my servers):

#!/bin/bash
echo "Attempting to start all servers"
bash /opt/opt/new_lobby/start.sh
bash /opt/opt/new_SMP/start.sh
bash /opt/opt/new_creative/start.sh
bash /opt/opt/network/start.sh
echo "All server start files have attempted to be executed"

If anyone has any ideas on this please help. Also I know this isn't "Ubuntu specific" but it's Bash script and I didn't really know where else to put this as it's Java, Bash, and Minecraft all together. P.S I know there are spelling mistakes in my scripts, but this was the first time I copy/pasted them into something that had an auto correct and I have yet to change them lol.

9
  • 1
    Have you tried "cd" to change directories before invoking screen?
    – mckenzm
    Apr 20 at 23:07
  • That wouldn't really work because I have to target multiple different jars in different directories. Also, you can't really use "cd" in bash script scripts. Apr 20 at 23:19
  • Why can’t you use cd in bash scripts?
    – Raffa
    Apr 20 at 23:25
  • @Chickblock2 you can use cd in bash scripts, they only take effect for the bash script execution and not outside it. When you are scripting, though, you should be using fully qualified path names. If they're all under a base folder (say /foo/bar/base/) and everything is under there then setting your script in the base folder and then using relative paths to the files from there may help. However, Fully Qualified Path Names are usually more useful here.
    – Thomas Ward
    Apr 20 at 23:28
  • Ok I'll try this; I've had weird problems in the past with using CD in bash scripts but then again, I'm fairly in experienced with bash scripts. Anyway, thanks I'll try this and see how it works. Apr 20 at 23:30

1 Answer 1

1

There are two ways to do this:

  1. Use fully qualified paths for execution (java ... /path/to/file.jar)
  2. Use cd in your scripts.

The first is self-explanatory - use full paths to your files at execution time.

The second is needed at each execution line for different directories to call cd /the/dir/to/enter before executing things. While cd does NOT affect the shell outside of the script, it executes in the shell that's spawned for the script, and has the same effect as you running cd on the command line directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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