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

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".

Related

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

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?

Link two processes Bash

I have two command line programs, a client application and a server application it talks to.
The client depends on the server running in the background. I can launch both via:
java -jar server.jar & java -jar client.jar
When I kill the client, however, the server remains in the background. Is there a way to link the two so that if the client dies, the server dies?
Run the server job in the background and store process id using $!. Then run the client.
After the client exits, kill the server using the stored pid, like this:
java -jar server.jar &
server=$!
java -jar client.jar
kill $server
shorter: no need to store PID of the background process, there's only one
java -jar server.jar &
java -jar client.jar
kill $!
There is no way to link them, but you can manage the processes explicitly.
java -jar server.jar & server_pid=$!
java -jar client.jar
kill $server_pid
The server runs in the background, then the client runs in the foreground. The script blocks at this point, so when the client exits, the script proceeds with the next command, which kills the server.

How to specify different nohup.out files for multiple JVMs running into the same script?

I have a shell script launching different Java JVMs in parallel and sequential, it looks like this:
(java -jar project1.jar
java -jar project2.jar) &
(sleep 60;
java -jar project3.jar
java -jar project4.jar) &
(sleep 120;
java -jar project5.jar) &
(sleep 180;
java -jar project6.jar
java -jar project7.jar)
I launch my shell script using the following command :
nohup sh launch.sh &
It generates a unique nohup.out file with the different JVMs outputs scrambled.
I'd like to have 1 nohup file per JVM (let's say nohupProject1.out, nohupProject2.out...) containing its specific output.
Is it possible ?
Maybe by adding some JVM arguments ?
Thank you

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