Академический Документы
Профессиональный Документы
Культура Документы
Running java applications in virtual machine without a terminal (Linux / UNIX forum at JavaRanch)
Java FAQ
Recent Topics
Register / Login
Author
Hello, I have this problem with my java application(s). I have them on my server machine that runs Linux. I connect to it using PuTTY. However, when I start up a java program it takes over the PuTTY terminal and is automatically terminated when I close the connection. So, is there a way to run a java application in the virtual machine without the output terminal, or make it so that it isn't terminated when the terminal is shut down? Any help is appreciated. - Sampsa Lohi
Julian Kennedy Ranch Hand Joined: Aug 02, 2004 Posts: 823
Try using Thread.currentThread().setDaemon(true). This is similar to using nohup to detach a Unix process from the shell that kicked it off. You may need to create a worker thread to do that, rather than using the main thread. The JVM will then terminate and you daemon thread will persist even
www.coderanch.com/t/110369/Linux-UNIX/Running-java-applications-virtual-machine
1/4
7/14/13
Running java applications in virtual machine without a terminal (Linux / UNIX forum at JavaRanch)
after your terminal session ends. I'm not sure where the output (stdout/stderr) would go though... Jules
Tom Hill Ranch Hand Joined: Aug 24, 2003 Posts: 115
I might as well as a similar question here, its along the same lines - lets assume that youve got some application that does loads of stuff - is it possible to 'mimic' closing the application - and then 'restart' it - without the process stopping. I guess its like getting the operating system to keep a reference to your application in memory or something?
Ernest Friedman-Hill author and iconoclast Marshal Joined: Jul 08, 2003 Posts: 24093
Julian -Actually, the purpose of "setDaemon(true)" is almost the exact opposite of what you've described: it arranges things so that the thread in question won't keep the JVM from exiting. If at any time, all the threads running in a JVM are daemon threads, the JVM will exit.
16
I like...
There's no way to arrange things so that any thread, daemon or not, keeps running after the JVM exits. Once the JVM process is gone, all threads go byebye. S., You have to run the program "in the background"; when you start the process, type "java Whatever &" -- note that ampersand (&) at the end, which runs the program as a background process. Note that if the background process tries to read from System.in, though, it will freeze. Alternatively, you can send a foreground process into the background using the "^Z" character -- type control-Z to stop the process and get back to the shell prompt, and then type "bg" at the prompt to move the program into the background. Anyway, once the program is running in the background, you can exit PuTTY and the program will continue to run. This isn't Java-specfic at all: it's just how Linux (UNIX) works. I've assumed here that you're using bash, the standard Linux shell; if you're using a different shell
www.coderanch.com/t/110369/Linux-UNIX/Running-java-applications-virtual-machine
2/4
7/14/13
Running java applications in virtual machine without a terminal (Linux / UNIX forum at JavaRanch)
[Jess in Action][AskingGoodQuestions]
Ernest: Thanks for the tip, I managed to make the program run in the background, but it still requires the PuTTY connection to run. For example, when I have the program running in the background and type 'logout' in the terminal, it clears the screen but the window stays open. When I close it, the program is terminated. Thanks anyway, good to know how to run programs in the background Edit: It seems that the program still uses the open PuTTY console for output for some reason. However, I was able to execute other shell commands after breaking the execution and typing 'bg' to move the program to the background. Beats me... [ August 20, 2004: Message edited by: S. Lohi ]
Julian Kennedy Ranch Hand Joined: Aug 02, 2004 Posts: 823
Hi Ernest, Thanks for the clarification. I did think about that as I wrote it - i.e. how could a Java thread run without a JVM; just thought it might spawn a new one... Getting daemon threads confused with daemon processes. Dim of me. So if the main thread (and any non-daemon threads) dies then the JVM exits and all the daemon threads die with it; I can see the sense in that. Jules
I heard of an application called 'screen' to start programs, log off, and later login, and join the 'screen' with the running program again.
3/4
www.coderanch.com/t/110369/Linux-UNIX/Running-java-applications-virtual-machine
7/14/13
Running java applications in virtual machine without a terminal (Linux / UNIX forum at JavaRanch)
Posts: 1923
I like...
I don't know how it works, how to setup, and didn't test it myself. But it might be what you're looking for.
http://home.arcor.de/hirnstrom/bewerbung
Stefan:
Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Similar Threads Maintaining session in a terminal services environment PuTTy backspace addShutdownHook (on Windows) Java program terminates Head First Java book, chapter 15 - help needed with running the chat terminals
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 08:37:01 .
www.coderanch.com/t/110369/Linux-UNIX/Running-java-applications-virtual-machine
4/4