• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

making a process user/system process

 
paddy Mahadeva Iyer
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guyz,
I need some guidance! i have a java process being initated from a batch file!! but when ever i am closng the terminal on which the batch file is executing the java process also shutsdown!! i need it to run even after i have closed the terminal!!

is there anyway i can do this!!!

thnx in advance!
rgds paddy!!!
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

If you're on a Unix-ish OS, check out the excellent "screen" utility. It allows you to decouple a terminal session from an actual terminal window, and can do several other nifty tricks as well.
 
paddy Mahadeva Iyer
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply. But i cannot let it to be OS dependend. Is there any piece of code that i can write which will help me avoid the shutting of java process when the terminal/cmd is closed?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It WILL be OS-dependent. Unix terminals and the Windows console simply are not alike.

The question boils down to how a process is run on an OS, and that has nothing to do with Java, and everything to do with the OS.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Running a batch file is itself OS dependent.
 
paddy Mahadeva Iyer
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup! i ahve .bat and .sh files for the windows and linux environment!

can somebody suggest what could be done if am on the windows environment. Say i close the cmd after executing the batch file and i do not want the java process to exit. My question is.. can i deal with this issue with a piece of code ?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In windows bat, I do this:

start javaw.exe MyProgram 1>myprogram.out 2>myprogram.err

The "start" command runs the rest of the command in a new process. Because javaw has no console, I redirect the outputs to files just in case something goes bad.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Provided the Java implementation on your platform has the relevant support (it should), you can use a very simple bootstrap static main method to load your program as a new (daemon) process in the OS. Look at java.lang.Process and java.lang.Runtime, and try running "java ..." as the execution command. The new process runs asynchronously in a new JVM, so closing the current JVM down (by exiting the bootstrap class) is no problem. Also, the new process isn't tied to the current screen, so it will continue to run even after you log off.

I've used this successfully on both Windows and Unix without changing the code between platforms: however, you may want to provide the execution syntax "java ..." as part of the bootstrap method's arguments from your batch file, so the program itself is devoid of OS dependency (e.g. location of installed JRE).

You'll probably also want to code someway to shutdown the service (from within the running process) and this is often done using an appropriate signal via sockets.

Alternatively, if you don't care about a neat shutdown, you should be able to kill the process at any time (using "kill" on Unix or the Task Manager on Windows).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic