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.
Related
I ssh into the ec2 instance using command prompt. I then launch the jar file from there. The web app runs perfectly from any device until the command prompt is closed. The site immediately goes down. The instance is shown as “running” when the site is down. ideas?
Well, ec2 is the virtual machine and it will show as running because you didn't shutdown or terminate it.
Your webapp is down because closing the command prompt will quit the shell session and thus terminating/killing the running jar.
It seems you are not running the jar as a background process.
If you are using Linux EC2 instance then try running your jar as
$java -jar jarfilename.jar &
The & makes your java process as a background job.
Note down the process id and then close the session. Now your webapp will keep on running as long as your ec2 instance is running.
I'd suggest reading about nohup and background processes in Linux in general.
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.
I am developing a java server-like application (though it is not a server itself, more like a bot for social network) and I decided to use Azure virtual machine to deploy the app on. So I've chosen Ubuntu virtual machine. I successfully uploaded .jar file on server, connected to it with Bash shell for Windows and SSH (as described in manual for Azure). Then I am able to launch my file with java -jar server.jar and it works. But the problem is that when I close the shell on my home computer, the app shuts down on the server too. So my question is how to launch .jar file in the way where it won't exit once I close SSH session?
Run the command in the background with nohup:
nohup java -jar server.jar &
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.
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>