Execute Command Line Java program in background - java

Is it possible to execute a Java program in the background so the user can execute other commands in front of it?
For instance, here is how the console might look for said program:
$ myProgram (executes program)
Program Started! (output from myProgram)
$ (the user can enter another UNIX command while myProgram is still running)
Thanks in advance!

Background execution is part of the shell. You can add & at the end of the command line to run it in the background.
The background output does not go to the current shell. If that happened, it would be confusing to the user, having to type input while the terminal is still producing output.
EDIT: I just tried "ls &" on cygwin, and the ls output appears in the console. Seems there is a lot of conflicting information on the net! :)

Best way is to use screen if you dont have it type
sudo apt-get install screen
type
screen
run the command like
java MyClass
press ctrl + (a + d)
to view this window again, type screen -x

I beleive it's possible to start the program and allow access to the shell... but the user would not see the output.
I do not think its possible to achieve the example scenario you have given.

Related

Having to hit enter with nohup command in bash

I'm trying to run a java springboot server using the following nohup command.
nohup java -jar webservices-0.0.1-SNAPSHOT.jar > nohup.out 2>&1 &
But after running this, I'm getting the following output & I have to hit enter to run the subsequent commands.
$ + nohup java -jar -Dspring.config.additional-location=config/application-dev.properties webservices-0.0.1-SNAPSHOT.jar
Is there anyway to avoid this in bash?
It looks like you have shell debug tracing (set -x mode) turned on, meaning bash will print each command before executing it (but after expanding variables etc). Since the nohup java ... command is backgrounded, this happens asynchronously, and happens to get printed after the shell has printed its prompt, so you get "$ " (your shell prompt) followed by "+ nohup java ... (the debug trace).
(Note: you have errors & output from the command redirected to nohup.out, but since the trace is printed by the shell, not the command itself, the redirect doesn't apply.)
You don't actually need to press return at this point; the only thing pressing return does is get you a new, clean (not mixed with debug tracing) prompt. You could just enter another command as normal. However, since the shell is confused about where on the line you are, features like command editing and history recall may not work properly.
If you want to avoid this, I think your main options are to either turn off debug tracing (set +x) before running the command, or add a delay before the shell prints its next prompt:
nohup java -jar webservices-0.0.1-SNAPSHOT.jar > nohup.out 2>&1 & sleep 1
Here, the nohup java ... command runs in the background as usual, but the sleep 1 command runs in the foreground and so the shell won't print its next prompt for a second (which should be enough time for the background process to print its debug trace).

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

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.

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 find out the command for one java running process?

I'd like to know how it is started. What is the command to start this java process ? What I mean is I have one running java process, and I'd like to know the command to start it, such as what is the main class and what is the arguments, etc.
Any tool for that ? Thanks
There is a command line tool that comes with the JDK: jps, that will give you the list of java processes being run at the moment you execute the command, the arguments given to the method main and the parameters used for the JVM. Try this:
path\to\jdk\bin\jps -m -l -v
It won't give you the exact command used to start the process, but it will give you a hint of how to "rebuild" that command.
For more info, if you are on a decent distro of linux, try man jps or if you are on Windows, see the Oracle documentation about jps.
Your question wasn't clear. If you are looking to find the command that launched this process than you can look at the property sun.java.command. This will give you the main class name and arguments passed to it. java.class.path property gives you the class path. You can get the arguments passed to the java command itself by using ManagementFactory.getRuntimeMXBean().getInputArguments() method. Using all these you should be able to reconstruct the java command.
If you use Windows, you can use the Taskmanager, go to the Process/Details Tab, where you can see the PID for each Process. There you can add a column for the command line (e.g. in German its "Befehlszeile", i'm not sure how that column is labeled in English).
Then just look at the java.exe/javaw.exe Processes.
You could also use the alternative Taskmanager from Microsoft, Process Explorer, afaik there you can just click right on a process and select details.

Silent mode execution with java.exe

I would like to know if there is a way to execute the "java.exe" as a background process (silent mode execution)
Ex: java -cp . MyClass arg1
I want to run the above statement as a background process , without opening command window
Under Windows, use javaw.exe instead of java.exe. See here for link, relevant bit copied here:
The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you don't want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails for some reason.
Use
javaw -cp . MyClass arg1
(assuming you're on MS windows)
If you're looking at services rather then applications then have a look at 'service wrappers'

Categories