Bash script does not end Java process clicking on the "X" - java

Can someone explain to me why this is the case. Let's say I have a simple java program that loops forever until I kill the process.
If I make a shell script like this.
#!/bin/bash
# clears the terminal
clear
# runs the java executable
java -jar java_program.jar
# prevents the terminal from closing
$SHELL
The shell script runs the program and if I use hotkey CTRL + C it ends the java process exactly how I want it. But when I use the "X" in the top right corner the java process does not end. Why does it do this how do I solve this issue?
I know the java process does not end because if I look in task manager the process is still running.
I am using windows 10.

I do not have git bash installed, but the behaviour you perceive comes from the terminal emulator that is used around git bash. It is not the windows native terminal emulator (and running cmd), it is not the cygwin terminal emuator.
Since you have it installed figure out the software emulating the terminal and see how that treats the processes when you close the terminal window. Maybe it is configurable.

Related

How to find and close running Win-Process which is Java app from within other Java app?

There is windows process which is actually a java application (java.exe). I need to close it from within other java application (other running .jar) which is probably will also be named as java.exe in windows taskbar.
To do the task we would normally do it this way
Runtime.getRuntime().exec(KILL + serviceName); // where serviceName is a `java.exe`
But there would be some java.exe processes in windows taskbar. So how to find the one I need? In this case it is forum.jar.
Actually this jar was started from .bat file (if it makes any difference) - it is process.bat as it seen in the screenshot.
You could run
wmic process where caption="java.exe" get commandline,description,processid
to get the list of processes and their associated command line like in your screenshot. Then you get the ID of the command you want to kill and kill it with taskkill /pid xxx.

DTEXEC doesn't trigger dtsx package in a power shell when triggered from java but works from command prompt on local machine

I'm calling a dtsx file in PowerShell(test.ps1) using below:
& $dtexec /f "$dtsx"
This works fine when I run the test.ps1 from command prompt in local machine but the same doesn't even trigger when test.ps1 is triggered from a java application.
please help.
I'd check if any other Powershell runs for You when started from Java, and only this particular one fails.
At some point I had similar problem. It turned out, that I was using Powershell module that required to 64bit powershell instance while I was running 32bit java process. This in turn was spawning 32bit Powershell process (You can check that with [Environment]::Is64BitProcess) that could not run what I was asking it to run.

How to run matlab on the background?

I'm currently using an external editor of Matlab .m files, with a custom build system that calls Matlab from the command line to run the Matlab script (with the -nosplash and -nodesktop). However this creates two problems:
1) Matlab closes right after running the script: any windows or plots I call in the script are closed right after running the script, which obviously happens in a matter of seconds.
2) There is a slight delay every time I run the script because Matlab is effectively being started from scratch.
So I was wondering if would be possible to have Matlab running in the background, and just running the scripts whenever I want?
I'm running Linux 64bits, Matlab 2013a, and Sublime Text 3.
EDIT: I've testing the setup with a basic script:
a=5;
figure
plot(a);
EDIT2: I'm calling Matlab through a Sublime Text build system that runs:
matlab -nosplash -nodesktop <[script].m
There is no way to have Matlab running in the background and "just running the scripts whenever you want" without having an interactive session open somewhere.
Suppose that your system has a custom wrapper matlab-wrapper that is used to submit scripts in the background. You would call your script like this:
$ matlab-wrapper myscript.m
Likely, matlab-wrapper is doing something like this:
#!/bin/bash
/apps/matlab14a/bin/matlab -nodesktop -nosplash -r run\ "$1",exit
Or even more, submitting the above script to a scheduler via qsub or some other command.
The key would be modify the wrapper script to find the part where the Matlab binary is actually invoked. If your system allows, you could copy the wrapper script and modify it. (Either by simply removing the -r run\ "$1" text or something more complicated.) Then, you should be able to launch an interactive version of Matlab per the custom configuration on your system, and call your scripts from the Matlab command window.

Running Java Infinite Loop in Ubuntu

Hello I am making a Java program in an Ubuntu Web Server but it is always supposed to be running in an infinite loop, or at least until I stop it. When I run it in the Ubuntu console it won't allow me to keep using the console. To work around this I have been using the "screen" command and detaching the screen.
I was wondering if there is a better way of doing this without working with the screen command?
Run a program immune to hangups
If you're happy with starting the Java program up manually from the command line, but just want to not have screen running, you can use the "nohup" command to start the Java process in such as way that the Java program will continue running even if you close the console window or log out.
$ nohup java ...
nohup: ignoring input and appending output to `nohup.out'
$
Run a program in the background
If you don't mind the Java program stopping if you close the console window or log out, you can skip using "nohup" and only append a "&" to the end of the command to tell your shell to run the application in the background. You may also want to add " > program.log 2>&1" to the command to avoid having any output from the program show up in the console window while you're using it for other purposes.
$ java ... > program.log 2>&1 &
[2] 3128
$ jobs
[2]+ Running java ... > program.log 2>&1 &
$
Run a program as a daemon
If you want the Java program to automatically start up every time you reboot the machine, you should look into creating a SYSV init-script, or as you're running on Ubuntu, an Upstart Job definiton.

How to start a java-web-start application and wait until it exits?

I just found out that javaws -wait doesn't do what is supposed to do on Windows. It will return before the aplication finishes.
This bug is more than 5 years old and closed as won't fix
6281477, so we need a workaround.
I looking for a clean solution that will:
run unattended (no user intervention):
currently if JNLP file is not accessible, Java will display an error window.
if application fails for any reason we need a return code different from 0
It would be preferable to have a cross platform solution for this Java or Python but it would be acceptable even a Windows batch solution.
If you don't need your application to be booted from the web (which, from your question, I assume is the case), use javaw instead.
I believe it will give what you want.
I know this question is pretty old, but here is an alternative to javaws -wait: javaws will start a javaw process with com.sun.javaws.Main as main class.
The jcmd can come to the rescue:
while jcmd com.sun.javaws.Main PerfCounter.print > /dev/null 2> /dev/null ; do
echo "still running !"
sleep 2
done
One down side of this is to determine the actual java cmd: if multiple webstart application are ran, then the command will exit only if all application are down.

Categories