Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Java console app as a daemon (background)

I've developed a Java console application that when start, open a console window and remain in foreground, i want to start that application in background .

Now i launch the application by this command line :

java -jar myapp.jar

Is there a way to achieve this behaviour ? It's enough change the command line parameter or i need to do some change on my code ?

like image 221
aleroot Avatar asked Jun 24 '11 19:06

aleroot


People also ask

How do I run a Java process in the background?

Solution. To fix it, append a & symbol to the end of the command, it executes the Java program in the background, and continue until it finished.


3 Answers

The answer is operating system dependent.

*nix: <your command> &
Windows: (opens a new console): start <your command>
Windows: (doesn't open a new console): start /b <your command>
like image 88
Steve Brisk Avatar answered Oct 18 '22 22:10

Steve Brisk


If you are doing this in anything unix based then you can append & to the end which will spawn a new thread and keept it running in the background.

java -jar myapp.jar &
like image 6
Amir Raminfar Avatar answered Oct 18 '22 22:10

Amir Raminfar


If you really just want it to run in the background, java -jar myapp.jar & will do the job. That way, it'll still die when the shell closes, but you can keep using your shell.

If you really want it run as a daemon, nohup java -jar myapp.jar & will do the job. That way, it'll continue to live when the shell closes.

If you want this to be reliable, you can prepare an init script or upstart job definition, or run it via Vixie cron(8) @reboot specifier to make it start at boot.

like image 4
sarnold Avatar answered Oct 18 '22 22:10

sarnold