Run a java program in backend - java

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

Related

Java app from Linux command line stops on exit [duplicate]

This question already has an answer here:
Run java application as background process via ssh
(1 answer)
Closed 4 years ago.
I use Putty to log on to my Centos server, and use:
java -jar app.jar
to run an app as ussual. My app runs ok, but as soon as I close putty it stops. I suppose I should open some kind of persistent terminal, but since there is no visual desktop I dont know how to do it.
You can use nohup command.
nohup java -jar app.jar &
Incase you need to see output/errors of your program then you can look into nohup.out file in current directory. Standard output(eg. whatever you print using System.out.println()) and error messages will go to this file.
For more details : run-bash-commands-background-linux
nohup "<command>" &>/dev/null & echo "PID to kill: $!"
redidirection to avoid creation of file nohup.out (IIRC), and pid to have somethin to kill later on (kill -term)

Logs not being created when running jar on remote machine via SSH

Assume the following setup:
MachineA: userA#
MachineB: userB#
I want to run my JAR on machine B:
When I run it like this:
java -jar myApplication.jar > /dev/null 2>&1 &
All goes well, and logs are being generated next to my jar file.
When I run it via ssh from machine A:
ssh userB#<ipB> "java -jar myApplication.jar > /dev/null 2>&1 &"
Application starts on machine B, but no log files are being generated.
Btw, the jar is a regular spring boot application using logback for logging.
Your application is being killed before it even has a chance to create a log file. It will probably work if you remove the final ampersand from your command:
ssh userB#<ipB> "java -jar myApplication.jar > /dev/null 2>&1"
With the ampersand present, the command is placed into the background. As the shell created by the ssh process has no foreground jobs it immediately closes the connection. When the connection is closed machine B kills all the command's child processes, including the Java application.
You could also use nohup to detach the java process, so that it doesn't get killed when the ssh connection closes. This has the added benefit that the application will continue to run even if your connection is interrupted:
ssh userB#<ipB> "nohup java -jar myApplication.jar > /dev/null 2>&1 < /dev/null &"
You might also find it helpful to check out screen and tmux.
OK, the application was running and the logs were being generated, just not where I had expected.
In myApplication configuration file, there was a property
logging.file=logs/myApplication.log
When I was starting my app via SSH, the folder from which was executed was /home/userB and not /home/userB/deployment/build.
For that reason, logs were not being generated in the same folder where the app was, but in userB's home folder.
The fix was to cd to specific folder, before starting the app.

Run java process as background process in linux

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.

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>

Linux Launch java program on startup (EC2 instance)

my server program needs to be launched on the startup of an EC2 instance. At the minute im just launching it from my SSH with the following commands:
java -jar ~/DocumentManager/DocumentServer-0.2.jar
I tried adding this to the .bashrc and /etc/rc.local files but they only seem to work when i ssh in.
Anyone know how to make it so an instance of my application is launched when the computer boots?
Thanks,
Ben
It's possible you can create a script java_server_launch.sh like this:
#! /usr/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
JAVA=/usr/bin/java
MY_SERVER=/home/your_username/DocumentManager/DocumentServer-0.2.jar
USER=your_username
/bin/su - $USER -c "$JAVA -jar $MY_SERVER &"
Put your script under /etc/init.d directory, and then use the command:
update-rc.d java_server_launch.sh defaults
more on update-rc.d command by using man update-rc.d.
Hope this help.
Regards.
Add ampersand(symbol '&') at the end of the command.
For example, in your case, java -jar ~/DocumentManager/DocumentServer-0.2.jar &
Old question, but my answer might be helpful for people who look in future.
You can also run your program as a service which automatically run on ec2 container reboot. Below link worked for me:
https://medium.com/#lizlieholleza/run-your-java-application-as-a-service-in-an-ec2-instance-amazon-linux-d7c7b4c0b2f4

Categories

Resources