Run java process as background process in linux - java

I am running my project as jar using java -jar command in Linux machine. As soon as this program run , It produces logs in another directory. Running my program this way requires me to keep the shell open. Now If I have to see the logs , I can't do that in the same shell. I am forced to do that by either doing the duplicate session or new session. Is there any way I can run the jar as background process and see the logs in the same shell ?

If you don't care about it staying alive, something as simple as nohup java -jar myjar.jar & should work. If you need it to be automatically restarted if it crashes or start automatically at boot, you'll want to look into something like systemd or monit.

Related

Running Java program in a Docker Image

So I have a Docker network that has a Docker file with a bunch of information. I have a java program that is going to bring up the enviorment and then produce several commands to run within this enviorment. To be clear, the first command I need to run is NOT inside the Docker enviorment. I am having some challenges with the Process and Runtime classes.
First, say I wanted my java program to launch a new gnome terminal and then run a command to get into the docker network. I have this command,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal"});
Gnome terminal sucessfully comes up but any additional arguments I give in this array are just ignored. For example,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","ls"});
Does not work. The command I ultimatly want to run would look something like this,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});
Second, Once I have this running, will additional commmands I run work within the Docker enviorment? I have a python file with a Stream handler that specifies the correct commands to run.
Other documentation on related issues was limited.
I made sure my code was wrapped in a runtime exception try catch and that I was running the correct .class file. Any help on this would be great!
Edit: I have also tried to run this in another linux terminal like Hyper and Tilda
I also am able to get a sudo sign in when I run the command like so,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","--","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});
However it closes immediatly after authorizing.
Okay this is what I was attempting to do.
https://www.atlassian.com/blog/software-teams/deploy-java-apps-with-docker-awesome
This site is outdated and I had to use this link for getting that latest version of the java PPA.
This process basically installs java into the docker contatiner so that I can run a java program that uses Runtime.

Run a jar file permanent on v-server

I just bought a vserver and now I'm trying to run a jar file on it permanently.
The problem is, that if I connect to my vserver via PuTTY, the sessions ends when I close the program and that kills my program. How can I open a terminal sessions where I can run my jar file and which never stops? I'm running Ubuntu 20.04 on my server
Try the following:
nohup [your command and parameters] &
nohup is a unix command that means 'no hangup', so it won't kill the session when you disconnect.
The & means 'run this command as a background process'. That will let you disconnect without having to kill the program.
Here's more info on nohup : https://en.wikipedia.org/wiki/Nohup
In the longer-term you'll likely want to install the app as a service to start when you reboot the machine. The way to do so will depend on the flavor of unix/linux you have.
Best of luck!
Use nohup, screen, tmux or create a systemd service unit.

Starting a java swing process on boot using Upstart -- No X11 DISPLAY variable was set

I have a runnable jar file produced from clojure code that I am running on an embedded system (udoo - http://www.udoo.org/), which is running a version of Ubuntu for its OS (udoobuntu - http://www.udoo.org/udoobuntu-the-official-udoo-linux-operating-system/).
The jar file contains a clojure library I wrote, that includes a some Java swing stuff.
Running the jar manually via the command line using:
sudo java -jar myjar.jar
works fine. The sudo is needed for certain usb device permissions.
My problem arises when I try to start the jar using an upstart script called testjob. The relevant part of /etc/init/testjob.conf looks like:
start on (desktop-session-start)
expect fork
script
exec ./home/ubuntu/start > /home/ubuntu/boot-jar.log 2>&1 &
end script
Where /home/ubuntu/start is the following shell script:
#!/bin/sh
sudo java -jar /home/ubuntu/myjar.jar
exit 0
When this runs, either manually via:
sudo start testjob
or automatically by rebooting the system, I get the following output to the log file:
Exception in thread "main" java.lang.ExceptionInInitializerError
... <bunch of meaningless classloading stuff>
Caused by: java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at sun.awt.HeadlessToolkit.getMenuShortcutKeyMask(HeadlessToolkit.java:236)
at seesaw.keystroke$preprocess_descriptor.invoke(keystroke.clj:25)
at seesaw.keystroke$keystroke.invoke(keystroke.clj:50)
at seesaw.keystroke$keystroke.invoke(keystroke.clj:49)
at seesaw.keymap$map_key.doInvoke(keymap.clj:107)
at clojure.lang.RestFn.invoke(RestFn.java:445)
at seesaw.widgets.log_window$log_window.doInvoke(log_window.clj:88)
at clojure.lang.RestFn.invoke(RestFn.java:457)
at physicloud.utils__init.load(Unknown Source)
at physicloud.utils__init.<clinit>(Unknown Source)
... 52 more
I don't actually make any UI calls, but the run fails on classloading of the library. I know that a simple fix would be to remove the Java Swing code from the library, but the library is generalized to allow ui output to capable machines, and therefore I am looking for a workaround. It doesn't make sense to me why the jar will run via java -jar but not in the script.
I tried different cases for the upstart script's "start on" condition, all of which produced the same result.
I tried setting the $DISPLAY environment variable in the upstart script before executing the jar, but to no avail.
I also tried scheduling the start script using crontab #reboot, but the error was the same.
Anyone have suggestions?
You can pass -Djava.awt.headless=true to java to allow running non-GUI applications that happen to depend on some UI libraries.
For example,
java -Djava.awt.headless=true -jar something.jar

How to open java program in other linux computer without terminal holding?

I have written a java program with jar file. The java program is to update status of linux server so it need to keep running, but the linux server is in data center, so I need to remote to server to open the program. I use ssh to login linux server. Use command of "java -jar file.jar" to run the program.
However, the java program of the linux server will close if I close the terminal in my computer. Since I cannot keep opening my computer, I wanna know how to open the java programming without holding my computer terminal.
you need to use nohup to keep the program running after you log out.:
server:~name$> nohup java -jar file.jar &
this will keep your program running
Two ways
One
nohup java -jar file.jar &
Another
java -jar file.jar &
In both cases your process will go in background however the process will terminate in the second approach when shell terminates in second case.
If this program is intended to be running on all your machines for monitoring purposes, you should be running it as a service from your server's init system (systemd for most systems these days). You can use the Java Service Wrapper or jsvc or write your own init script.
Another solution apart from the proposed one:
screen -d -m java -jar your.jar
You will then have a detached screen with your java command in it. List with screen -l, reattach with screen -D -RR <screenid_obtained_via_screen_-ls>

Run a java program in backend

Hi all i want to run a java application as backend process.that is like tomcat server.For that i had developed one application.and made one class as main class and calling from one script file .i.e(startup.sh) file.in startup.sh file i was calling one class.that is MainMethodClass.In main method class i had written my business logic.when i am running this app in linux server from using putty is is working until putty window is not closed.As closed after putty window it is also stopped.but i need to run this app even i closed also.How can i achieve this.
Nohup will detach a process you run from your current console and let it continue when you close the terminal. Run something like this.
nohup java -jar my.jar &
By default it will pipe the output to nohup.out, so if you don't want that you could try:
nohup java -jar my.jar > /dev/null &
This problem is not related to java, its actually something related to the way linux operates.
You need to do following:
nohup <your_application_command> &
Note the "nohup" and "&" at start and end respectively.
You should be able to do something like:
nohup java -jar MyApplication.jar &
On a linux machine you can create a service for your jar( executable jar like spring boot )
# Set the Application as Service
ln -s $APP_BASE/bin/$APP_NAME.jar /etc/init.d/$APP_NAME
echo "Starting the application as service"
service $APP_NAME start

Categories

Resources