I would like to run a Minecraft server in the background with Ubuntu, but every time I do it stops running.I am also running bash as my shell.
java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui &
When I run jobs I get this at first.
[1]+ Running java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui &
Then this a few seconds later.
[1]+ Stopped java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui
Later I found out that it will stay "Running" if you redirect stdin/stdout/stderr to some files.
java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui > out.txt 2> err.txt < in.txt &
It works great, but I want to use the in.txt file as input for the running java process. When I first run this it executes the in.txt once. So if I have /stop in in.txt it will stop the server. But ONLY once. I would like to put commands in the text file and have that passed to the minecraft server. The file would become my keyboard input while the process is running. I know I can use screen, but I want to see if I can run it with a file as input. Again the in.txt would be my input.
So I could do something like this.
echo -e "/stop\r" > in.txt
OR
echo -e "/time set 0\r" > in.txt
Another rabbit hole.
I even tried making my own pipe. This makes minecraft run in the background.
mkfifo pipe;
java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui &> out.txt < pipe &;
cat > pipe
If I cat > pipe my prompt is taken again, but I can enter commands and it executes them as stdin. So if I do /say hi it will run it. Awesome! One problem. I need my bash prompt back. If I close it by using Ctrl-C, I'll lose my stdin forever! I can keep it around by Ctrl-Z. Later If I need it I'll fg. But that is very crude. I would like to run a script that would stick something into that at a later time. Something like echo "/say hi" > pipe. I lose stdin if I don't keep something running. echo runs once. That's the problem, it collapses stdin.
I am a novice at bash scripting so a little help will be greatly appreciated.
You already got the idea of using a FIFO. You need to run it with the following command:
while true; do cat pipe ; done | java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui > out.txt &
The while loop will make sure that even if the process you use for writing to the FIFO closes the file, the JVM standard input will not be closed. It will just start catenating pipe to it again and the while`s standard output is considered one long stream.
Related
I wrote an application in java that needs five players and a server.
I need to write a script that executes the jar of the server and of every single player in different terminal windows. How can I do?
I tried a script and worked but the jar opened in the same terminal window than I tried with xterm or konsole with flag --noclose but does not work (warning command: konsole not found)
#! /bin/sh
xterm --hold -e java -jar /Users/Marco\ 1/Documents/ing-sw-2019-Lentini-Marazzi-Marini/out/artifacts/server_jar/adrenalina.jar
for X in $(seq 5)
do
konsole --noclose -e java -jar /Users/Marco\ 1/Documents/ing-sw-2019-Lentini-Marazzi-Marini/out/artifacts/client_jar/adrenalina.jar gui
done
exit;
To run a process in the background from bash, you'll need to add an & to the end of your command, e.g.
java -jar /path/to/jar/my.jar &
Otherwise bash will wait until the command execution terminates.
I am using ubuntu 14.04
I am running a jar file which should be collection a large amount of data for a few days.
I am running the jar file thought this command and it works fine.
java -jar xxx.jar
However when i close the putty, the process stopped. Is there a way for a jar file to run even when i close the putty?
You can use nohup to run the jar(any process) in background.
Use the following command in the putty session :
nohup java -jar xxx.jar &
You need the nohup command. This command makes processes keep running despite closing terminal.
Run your jar with (in case you are in the right folder):
nohup java -jar xxx.jar &
I would suggest to you use
nohup java -jar xxx.jar > /dev/null 2>&1 &
which redirects standard error & output of the command to /dev/null which means it's discarded. If you need the console output of this command then you can redirect it to any file as follows
nohup java -jar xxx.jar > output.log 2>&1 &
I am running java -jar xyz.jar command on the linux terminal. I have both system.out.println statements and System.exit statements in the code.
how to capture the system.exit or output to the OS on the linux? do i need to prepare a linux script for this?
This may be off-topic for SO.
You just redirect it:
java -jar xyz.jar > the_file_to_output
If you used System.err output as well as System.out output and wanted to redirect both, it's a tiny bit more complicated because you need to redirect out to the file and then redirect error to out:
java -jar xyz.jar > the_file_to_output 2>&1
The 2>&1 thing is what redirects error (standard stream #2) to out (standard stream #1). Note that the order matters, you have to do that after redirecting out to the file.
Java works as any other command.
If you want standard output (or error output) you can work with usual I/O operators: > 2> >> |
java -jar xyz.jar > output.file
java -jar xyz.jar | sort | less
For getting System.exit value you have special variable $?
java -jar xyz.jar
echo $?
To get more info Exit status and I/O redirection
I am working on a minecraft control panel in Ubuntu and thus I need to start/stop a .jar filewith shell_exec();
When I try commands like "whoami", the output is normal. But when I try this:
shell_exec("screen -dmS mcsrv java -Xmx512M -jar /var/www/srv/craftbukkit.jar -o true nogui");
It does not do anything, I have checked the permissions too and www-data is the owner of the files
Try to redirect the standard error stream to stdout (by appending 2>&1 to the command), fetch that output and print it to check whether there was a meaningful error message
$cmd = "screen -dmS mcsrv java -Xmx512M -jar /var/www/srv/craftbukkit.jar -o true nogui";
$redirect = '2>&1';
// using variable substitution only for readability here
shell_exec("$cmd $redirect", $output);
var_dump($output);
I'm trying to get the PID of a java process started with xvfb-run. When started without xvfb-run, I use $! to get the PID of the last backgrounded process but as soon as I use xvfb-run I obviously get the PID of xvfb-run.
Here is the code:
#! /bin/bash
logfile=/var/log/SleepTest.log
pidfile=/var/run/SleepTest.pid
command="java -jar /data/test/SleepTest.jar"
( eval exec -c xvfb-run $command < /dev/null >> $logfile 2>&1 ) &
$! > $pidfile
If I remove the xvfb-run part in the second last line, everything works ok (except the part that I don't have a display and the program crash). I probably have to play with the "()" and "&" but I'm not an expert.
The program SleepTest.jar is a small program I wrote so I don't have to deal with the real thing. It only sleep for 2 minutes.
For those wondering why I use xvfb-run, it's because the java application I need to start use SWT and I don't have display on my server.
For those wondering why I need the pid of the process, it's because I want to create a init.d file to be able to start|stop|status my application
So is there a simple way to get it ?
Run your entire script with xvfb-run (e.g. xvfb-run name_of_script.sh), and remove the xvfb-run from your eval line.