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>
Related
I am making a website which requires a java program to run on the server (it is a ubuntu server). I want to achieve this by executing it from php. I tried 'shell_exec' and 'exec', but they both don't open a terminal window or execute the jar file. It did work on my windows pc, but I want to have it work on my linux server as well. I am using xampp as server.
The command I used that worked on windows:
shell_exec("java -jar PATH/TO/JAR/FILE.jar PARAMETERS");
I fixed it myself by adding '2>&1 &' at the end of the shell_exec() command. This makes the php script wait untill the program is finished.
The new command:
shell_exec("java -jar PATH/TO/JAR/FILE.jar PARAMETERS 2>&1 &");
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 a java program (text-based no GUI) that I have written and compiled and uploaded to a server.
I run it with java -cp myjar.jar mypackage.MyClass which starts it running processing a datafile with 20,000,000+ entries in it and printing output to System.out. I have calculated that it will take a very long time to process the data and I didn't want to have my laptop open for the 10 days of number crunching...
When I log out of my shell however, the process stops.
How can I execute the command and log out without it stopping? What is that even called?
I am using an Amazon Ubuntu EC2 server. I log in using a certificate from Mac OSX with terminal. The server seems to be using a bash shell.
Hope someone can help me out!
Jason.
Consider using screen instead of nohup. It allows you to create a virtual terminal that persists even after you logout/disconnect. When you reconnect to the server, you can immediately jump into the screen session you last had open.
Typical workflow on the server:
type screen (you may need to press space to leave intro page)
type in your command that you want to leave long-running (your java program, or an OS upgrade)
press ctrl a+d to leave screen (make sure to hold ctrl down)
To re-enter screen just use screen -r, and you will see the previous terminal and any running programs as you left it.
You can use nohup
nohup java -cp myjar.jar mypackage.MyClass > yourLogFile.log &
-----> http://ss64.com/bash/nohup.html
I was wondering if it was possible to execute commands from PHP to a Java prompt which is already running?
I have tried the solution listed here:
How to run a shell command through PHP code?
and this provided no functionality
Let me explain
The java is running on one screen of the linux server
sudo apt-get install screen
and running the .jar file through the command line.
I am then running a webserver, which will have an admin accessibility to restricted areas, which will contain scrips to run specific commands through that already running .jar file?
You can implement some kind of IPC. The java file listens to a port and receives the commands. Or you can write the commands in a specific file which the java programm reads. I think under linux you can also use shared memory: http://www.php.net/manual/en/book.shmop.php
It is possible by sending the command to the screen session. I used this for a minecraft server once.
screen -S <sessionname> -X stuff "<command>\r"
This would (IIRC) provide the same output as if you where inside the screen, typed the command and pressed enter.
I hope this was what you wanted.
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