How do I see output from a Minecraft server terminal when running a bash script? - bash

When I run this command in the directory my minecraft server (fabric modded) is in, I can see my output.
java -Xmx6G -Xms1024M -jar fabric-server-launch.jar nogui
Screenshot of Terminal Output
However, when I run my bash script in the same directory, even though I can still input regular server commands and have them affect the game, I can't read anything.
#!/usr/bin/bash
$(java -Xmx6G -Xms1024M -jar fabric-server-launch.jar nogui)
Screenshot of Terminal Output
Is there any way I could alter my script so that it may display server messages?

Related

Starting Django server from bash script doesn't persist

So this seems to be a relatively common occurrence (lots of similar questions have been asked like here , here and here) with some variations. I have execute permissions for the script and have tried all the solutions mentioned in those questions but it's still not working. When I try to run the following command to start my Django server, the bash script opens and closes. How do I get it to persist after starting the server?
startserver.sh:
#!/bin/bash
python3 manage.py runserver 7888
So the command runs when I type it directly into cmd (in that directory), but when I try to run the bash file it just flashes and disappears. I have tried running it as
.\startserver.sh
. startserver.sh
sh startserver.sh
start startserver.sh
but each does the same. Ideally I would like to have it such that you can double click on the server.sh file and it runs persistently. Anyone have any ideas why this is the case? I feel like it's something very small I'm missing.
You need to add the nohup to your script.
Change
#!/bin/bash
python3 manage.py runserver 7888
to
#!/bin/bash
START=`pwd`
nohup python3 manage.py runserver 7888 >"${START}/startserver.LOG" 2>"${START}/startserver.ERR"
You don't want to rely on the longevity or continued existence of nohup.out because that default destination can be easily clobberred by other programs.

Start java in windows batch and kill it when timeout, need output java console to a file

In my batch script, I need to run java automation and kill the java process when timeout.
run java
loop to verify if java process exist, if still running during timeout then kill the process.
When I use start java, then the java execution won't output to the console file.
start java -Xms64m -Xmx1024m -cp my.jar 2>&1 >Console.txt
When I remove start, the console could be generated but it will pending at java command, it will not execute to the verify after.
java -Xms64m -Xmx1024m -cp my.jar 2>&1 >Console.txt
I just want to share I use another workaround to realize. What I am using is jenkins job, I use parallel step to get it worked. One job in parallel is to execute "java -Xms64m -Xmx1024m -cp my.jar 2>&1 >Console.txt", the other job is to execute "verify if java pid still running, if running then loop wait, when timeout, kill the pid".

Keeping access to STDIN of a program run in the background via a FIFO

So I have a docker image, containing a minecraft server. A minecraft server takes input from the STDIN, so after starting the server with java -jar server.jar I can feed it commands (to stop the server for example) by typing it in the console.
Ideally I want to start the server in a shellscript that looks something this:
#!/bin/sh
.. some initialization ..
exec java -Xmx$RAM -Xms$RAM -jar server.jar
This way the java executable takes over the process, so that it can be gracefully terminated when docker sends a SIGTERM.
The issue comes when I want to keep an artificial STDIN, that allows me to executes commands onto the server via another script. To do this I made a FIFO that represents the console like so:
rm -f console; mkfifo console
I'm not sure how I can redirect this FIFO into the process this way though. I'm reading the fifo using cat in a while-loop like so
while true; do cat console; done
And thought about doing something like this:
while true; do cat console; done | exec java -jar server.jar
Or something like this:
exec java -jar server.jar < <(while true; do cat console; done)
For the latter, I've looked at bash: pipe data into an exec'd command which unfortunately gives me syntax error: unexpected redirection
I've also tried this:
while true; do cat console; done >&0 &
exec java -jar server.jar
which seems to freeze the server while it's starting.
I have no idea why all of this is not working, and I don't know how I can make it work.
To be clear of what I want to achieve:
Have java (the server) take over the main process
Have a FIFO redirected to the STDIN of the server
It sounds so simple to me, but I just cannot make it happen. Any help would be greatly appreciated.
I just found out that the < <(command) syntax is not supported in /bin/sh but it is support in /bin/bash.
After switching to that, my final line is:
exec java -jar server.jar < <(tail -f console)
Sometimes you just have to accept that you have to install more dependencies..

Running one command at a time in a shell script

I am using Mac OS and zsh. I am running a shell script that launches several Java programs. They terminate once they have created their output (they are essentially scripts). However, it seems that my current script starts all the Java programs at once, which is very resource-intensive.
Currently my shell script looks like this:
java -Xmx2048M -jar gha.jar params1.yaml
java -Xmx2048M -jar gha.jar params2.yaml
java -Xmx2048M -jar gha.jar params3.yaml
When I run it, I run out of memory. How can I modify my script so that it only launches the next Java program once the first one has terminated, so that memory is refreshed in between?
You are mistaken. The shell will run these one-at-a-time. The only possible explanation is if these programs launch background processes, in which case the shell cannot know how to wait for them to complete.
As #bmargulies mentioned above your command as you have show it should run one command at a time.
Try modifying your shell script as as shown below. Using the construction below a command is run only if the previous command completes successfully (in theory at least).
java -Xmx2048M -jar gha.jar params1.yaml && java -Xmx2048M -jar gha.jar params2.yaml && java -Xmx2048M -jar gha.jar params3.yaml
If this does not work, run the script in the background and list out the currently running processes. Hopefully that'll give you an idea of what's running on your system.

What's the nohup on Windows?

I want to run a Java jar file like this:
java -jar spider.jar
How to run it on the background on Windows?
Like this on Linux:
nohup java -jar spider.jar > /var/tmp/spider.log 2>&1 &
You could use the Windows start command:
start /min java -jar spider.jar
This command is not really the same as nohup; but it might be suitable if you're happy with the Java process running in a separate minimised window. See http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx
On Windows it's not normal that a process terminates once its parent was killed (like Unix-likes do it normally). Therefore there is no direct necessity for something like nohup. If you want to avoid the console window associated with it, you can use javaw but redirection won't work, then.
The only way to get the nohup behavior, where the process still runs after logging off (like for micro-services, analytic tools, server batch jobs etc.), is to run the .bat file with the start javaw -jar ... contents as either a service or a scheduled task.
save the following code to nohup.vbs, and add the directory to PATH.
Set args=Wscript.Arguments
Set ws=CreateObject("wscript.shell")
ws.Run args(0),0,true
then, run:
nohup "java -jar spider.jar > /var/tmp/spider.log"
Note that you should quote the full commands and the redirects.
For windows run following mentioned command in Command Prompt or in Terminal
nohup (file need to run) > (File you need to save the log) 2>&1
Ex:
nohup ./bin/windows/kafka-server-start.bat config/server.properties > ./MyKafka.log 2>&1

Resources