Link to home
Start Free TrialLog in
Avatar of beside
beside

asked on

Java program as deamon

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?
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
why can't you just run your app in the background?
Avatar of johnchan2000
johnchan2000

Try using javaw?
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!
Or the service wrapper... which is the nicer, cross platform way of doing it... ;-)
> Just forking off or running& in UNIX

How does at and batch background the process?
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!
I realise that, but under the hood how are the process backgrounded?
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!
Avatar of beside

ASKER

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.
> 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
can you explain what the problem is with running the process in the background?
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)
Why the C grade?  Seems a little bit harsh... :-(
Especially as you never explained WHY the service wrapper was not a valid answer...
Avatar of beside

ASKER

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.
>  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? ;-)
Anyway...good luck with it...

Tim