restart console on Minecraft Server - java

Please excuse me for my inexperience, I am very new to this.
I set up a server on Digital Ocean for Minecraft earlier today. I ran the command to create the world with java -Xmx1024M -Xms1024M -jar minecraft_server.1.15.2.jar nogui, and created a screen.
It gave me access to the console where I could type like /op and other commands. Later on, I shut down, and now when I try to rejoin, I'm not sure how to get back to that console. I have more than 1 person on the server, who is on the console right now - I don't know if that should cause it any issue. When I retry to open the console with the command java -jar minecraft_server.jar It says there is already a server running (which there is) but I just want to get back to the console.
Thank you in advance.

If this is a linux vserver or root server, you can use the "screen" tool to access the minecraft console again.
Create a new startup script (start.sh) in the server directory to launch the JAR:
#!/bin/sh
screen -d -m -S "minecraft_screen" java -Xmx1024M -Xms1024M -jar minecraft_server.1.15.2.jar nogui
Open your terminal and execute the following in the directory:
chmod +x start.sh
Run your start up script:
./start.sh
To access the minecraft screen use the command "screen -r".
To leave the minecraft screen press Ctrl + A + D

As a player logged into the server, stop the server by typing /stop. This will stop and close the server. Now proceed back to your hosting service and start the console the same way you did the first time. If it worked this way the first time, i can imagine itll work the second time.

just noting in case this becomes a resource for other new server owners, as of writing this comment - screen has been depreciated and tmux is it's replacement
use 'tmux' to create a screen
use 'ctrl+b' then 'd' to detach
use 'tmux attach' to rejoin a screen
i spent a little while searching the internet for this, so i thought sharing it here would be logical - apologies if this question is too outdated to still be relevant

Related

Screen -dmS working from command line but not from Java (Spigot restart script)

I recently changed my dedicated server and since then the script I use to restart my minecraft server isn't working anymore.
I can call the script using sh start.sh and my server starts, however when I use the in-game /restart command or when I instruct the server to restart from my plugin, the server closes and never restarts.
Here is the content of my start.sh
screen -dmS mc_hub1 java -jar -Xmx2048M -DIReallyKnowWhatIAmDoingISwear server.jar
Unless I'm running the script from the command-line, nothing happens, the screen isn't even created.
The issue seems to be originating from screen -dmS mc_hub1 because the restart instruction works if I remove it, actually it feels like I can't use anything related to screens from Java but it used to work so I'm a bit lost.
The dedicated server on which everything works is running Debian 9.9, Screen 4.05.00 and the new one is running Debian 9.11 and Screen 4.05.00.
I tried to add the -L option to enable logging but it doesn't even create the file,
Everything in the folder has read and execute permissions,
I tried to call the script from my plugin using java.lang.ProcessBuilder, no exception but still no result
ProcessBuilder pb = new ProcessBuilder("start.sh");
pb.directory(new File("/home/minecraft/uhc/"));
pb.start();
Java version doesn't seem to be causing the problem (I tested with the latest one and the one from my first dedicated server which is older)
Any help would be appreciated, thanks

Launching java as a service and specifying the user

As an introduction, let's just say I'm a real noob using linux. I try to do the things right, don't hit me if it's ugly.
So, the problematic. I'm trying to run some jars as webservices on an ubuntu server. I created a specific user (nuxservice) with no pwd. I edited sudoers to enable a few users (myself & root) to sudo using this account with no password.
I then took a lot of inspiration from : http://www.jcgonzalez.com/linux-java-service-wrapper-example
Most is working, only one real problem, my java process seems to not have the right to create its logging files.
I run my services with a classic
sudo service myservice start/stop/restart
The command line that are launching my services are :
nohup sudo -u nuxservice java -jar myjar.jar myargs
When I do a ps -ef, the services are launches with my nuxservice user.
When I do ls -ld, nuxservice is the owner and have the rights.
If I launch the command in a terminal myself, it works. When launching as a service, my logs files are not created.
Any clues mates ?
So...
It was kinda silly.
My user rights were fine. Problem is, I did not set the working folder in my script so Java was all lost considering the creation of the folder/files for logging.
All I had to do was adding a little
cd $PATH_TO_JAR
And it was all set !

Java console running in a screen autostart on Ubuntu

[Ubuntu 14.04] I have a java console application, and I need to run all time (1st problem), as well as I can re-access the console whenever I want (2nd problem).
To solve the second problem, I use a screen, then run the jar file in it. So that I can re-attach the screen to access my console app.
I am now stuck with the 1st issue. I want to make the screen autostart with OS. Because I need the app running all time. Anyone give me an idea? I appreciate all your help. Thanks.
UPDATE 11/17/2015:
With #janos's help, it works fine except using #reboot. I tried crontab starting the script each 5 minutes and it worked fine. But when i replaced it with #reboot, it did not work anymore!
Finally I chose this solution: Create a screen with a particular name, then create a script sh file to check if the screen with that name existed or not. If not, then run the screen along with java file. Last, create a crontab to run the script each 1h.
Many thanks to #janos for your effort and help.
To run a program after system boot, use #reboot in your crontab:
#reboot /path/to/executable args
For more details, see this Ubuntu help page:
https://help.ubuntu.com/community/CronHowto
As per your comments, you seem to be having difficulty running Java + screen with cron. To help you debug, I suggest to create a custom configuration file for screen, let's call it ~/screen-debug, with a content like this:
screen -t home
screen -t java bash -c 'java -jar ...; echo Press enter to exit; read'
And use a crontab line like this for testing:
*/5 * * * * screen -c ~/screen-debug -d -m -R java
What's happening here:
Run the job every 5 minutes
Use a specific screen configuration
Start screen in detached mode
Reuse the screen session named "java"
If a session with this name doesn't exist, it will create it
If a session with this name doesn't exist, it will reuse it (not start another screen)
There will be two windows in the screen session:
Labeled "home": a simple shell, as if you run screen in your home directory
Labeled "java": the Java program, hopefully happily running. If not happily running, you should see the error message that should help you debug the problem, and a prompt to "Press enter to exit". When you press enter, the shell will terminate in this window.
Once you get this working, then you can replace */5 * * * * with #reboot.

How to catch Java application output (System.out.println()) when connected to remote host (OS Solaris) via Putty and connection is lost?

How to catch Java application output (System.out.println()) when connected to remote host (OS Solaris) via Putty if remote session is lost and connected again? Java application on remote host was started like this:
java -jar /SomeApplication.jar &
Now the output is visible. But if I exit remote session and connect again - no more output is visible, though the same java application is still running - I can see it by issuing this command:
ps -ef | grep java
Output is:
root 5221 1 0 20:04:15 ? 1:20 java -jar
SomeApplication.jar
The application ID is 5221 and it is running, but it's output vanishes somewhere..
A quick and neat suggestion is running the application in a screen session (check the Solaris package).
First, you launch screen:
$ screen
Then you launch the application inside screen, like usual:
$ java -jar /SomeApplication.jar
And you are set. You don't need the trailing &: you can detach (see below) and log out; the screen session will be kept running. On next login, just issue:
$ screen -x
and the session, with its output, will be reattached.
A quick reference:
Use CTRL+a d (first CTRL+a together, then d) to detach from the session, so that you leave the screen session (with the java application running inside), go back to the Solaris shell and do other things -- including logging out graciously.
Use CTRL+a ] to enter "copy mode", so that you can scroll up and down and q to leave copy mode.
There are many other useful features, just read a quick intro :)
Log files play better in these scenarios.And sysouts are expensive operations too.
Or when you start the java program, redirect error/outputs to a temp file. So you can view it later.
java -jar /SomeApplication.jar > wherever_you_want_output.log 2>&1 &
the > redirects system.out.println to the file, the 2>&1 sends the system.err.println to the same file (you could also put 2> error_file.log to get a separate System.err.println() output.
Long term though you should use a logging framework through like log4j or the java logging framework.
If you want to watch the output then as it grows, you can do a tail -f whatever_you_want_output.log

Start Minecraft_Server.jar via PHP Exec() or similar function

I'm trying to build a script that starts minecraft_server.jar (location: user directory / mineserver / minecraft_server.jar ).
I have PHP and Apache installed, and I'm trying trying to start the server JAR from /var/www/html/interface.php.
Via console, the server starts fine by running:
java -Xmx1024M -Xms1024M -jar mineserver/minecraft_server.jar nogui
... from the user directory. So in the interface.php file I have the following (note location listed above):
system('java -Xmx1024M -Xms1024M -jar /home/ec2-user/mineserver/minecraft_server.jar nogui', $retval);
But the server never starts after I visit the file. What am I doing wrong?
Thanks for any and all clues.
I want to say thank you for your help on Thanksgiving. While I haven't found the solution yet, I appreciate the efforts. Thanks again.
Have you checked the error log? It's at /var/log/httpd by default. If anything went wrong, it should be there. Otherwise, it could be starting wrong and I would recommend launching it in a screen so you can hop in and check on anything it's doing.
Install screen through whatever method your distribution uses, and then change the launch string to be:
screen -dmS "minecraft" java -Xmx1024M -Xms1024M -jar /home/ec2-user/mineserver/minecraft_server.jar nogui
The command will be executed with the permissions, and the environment of whatever user account the web server runs under.
In particular, it may have a different PATH entry, and the java command might not even be found, or it might be found somewhere else etc...
Also, be aware of what current working directory it is inheriting from your PHP script.
See exec() and check the output to help yourself do some basic debugging.
Also, you probably want something more like
exec('nohup java etc... > /dev/null 2>&1 &');
so that the process gets properly backgrounded and disconnected from the webserver parent process.

Categories