Solved

Java program as deamon

Posted on 2004-08-11
20
351 Views
Last Modified: 2010-03-31
Hello,

I want to make my program as deamon:

I run the program, and it gives me again ready command line, but program runs in the background. Like in C on UNIX I can fork new process and close parent, this gives me console free, and runs program as deamon.

Is it possible on Java? If yes, then how?
0
Comment
Question by:beside
  • 7
  • 4
  • 3
  • +3
20 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 100 total points
ID: 11774104
0
 
LVL 92

Expert Comment

by:objects
ID: 11779171
why can't you just run your app in the background?
0
 
LVL 1

Expert Comment

by:johnchan2000
ID: 11780512
Try using javaw?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 24

Expert Comment

by:sciuriware
ID: 11781605
Just forking off or running& in UNIX is a bad idea: the process can accidentally be killed.
Use
        at now
or
        batch
in such a case.

In general a JAVA program can NOT fork, but spawn any process on any OS in the background.
;JOOP!
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11781620
Or the service wrapper... which is the nicer, cross platform way of doing it... ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 11781985
> Just forking off or running& in UNIX

How does at and batch background the process?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 11782111
at and batch send the command to cron ......

Practice:
> batch
sleep 2000
Ctrl/D

And find a kind of batch file (=shell script) in /var/spool/cron/atjobs/...  (or such, I've not looked at it for 10 years).
See how it reflects your foreground.

;JOOP!
0
 
LVL 92

Expert Comment

by:objects
ID: 11788963
I realise that, but under the hood how are the process backgrounded?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 11790963
the cron daemon spawns the mentioned scripts.
They run under the effective user who submitted, but without terminal connection
and thus without vulnerability to signals from a keyboard session.
;JOOP!
0
 

Author Comment

by:beside
ID: 11851013
no one of answers gave me a straigth or moreover straigth way to solve the problem, I need to do deamon with my java code, not service wrapper, javaw exists only in windows sdk.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11851390
> I need to do deamon with my java code, not service wrapper

why not with service wrapper?

That's the only cross platform way of doing it...

On windows, you have javaw, on linux, you have  "nohup java MyClass &"

Javaw will stop when you log out, nohup on linux wont (if you leave off nohup, it will) ;-)

Tim
0
 
LVL 92

Expert Comment

by:objects
ID: 11856921
can you explain what the problem is with running the process in the background?
0
 
LVL 2

Expert Comment

by:_kiew
ID: 11865708
You do need to use the service wrapper if you want to be able to stop your daemon cleanly, running the process in the backgtround will be of no help for that purpose.
TimYates' wrapper is just fine, you'll then just need two classes, one to start the daemon and another to stop it (can be the same with different arguments)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11901352
Why the C grade?  Seems a little bit harsh... :-(
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11901370
Especially as you never explained WHY the service wrapper was not a valid answer...
0
 

Author Comment

by:beside
ID: 11901436
I explained, that I need not a wrapper, but JAVA code to start my program as a deamon. But your answer is accepted, because all other explains about running in background with & or javaw is not running as deamon.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11901448
>  I explained, that I need not a wrapper, but JAVA code to start my program as a deamon

As I explained, the only way to do this in a cross-platform way, is to use the Service Wrapper...  

So maybe the correct answer of "you can't" would have got me a higher grade? ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11901452
Anyway...good luck with it...

Tim
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
more than one jdk and one jre 1 50
hibernate example using maven 12 56
Convert from a json string array to a Java object 3 52
eclipse console opening separately 2 29
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question