Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a java program without a console [closed]

I need to run an java program even the terminal is closed.... in server....

like image 490
Ramesh Avatar asked Jan 22 '23 19:01

Ramesh


2 Answers

On Unix and GNU/Linux systems you can run the program using nohup like this, assuming it is a jar:

nohup java -jar program.jar &

To get the output of the program run into a text file so later on you can view it, you can do:

nohup java -jar program.jar > program.log &

There are packages that will wrap your Java programs into services too, which is more manageable than bare java processes.

You probably also want to use a "process wrapper" (Launch4J maybe?) to give your process a meaningful name, otherwise all your Java programs will appear as java in the process list, which isn't very indicative.

like image 124
bakkal Avatar answered Jan 24 '23 10:01

bakkal


An "alternative" to nohup would be screen. screen is very useful and allows you to run any task, detach the screen, and let it run in the background. You can resume it later.

To run a task:

screen <command_you_want_to_run>

Then <ctrl> <a> <d> to detach from the screen session.

The next time you log in you can reattach to the screen session with:

screen -r

If you have multiple screen sessions running you will be presented with their details and can connect to them like this:

screen -r 1234.pts-1.hostname

... where 1234.pts-1.hostname is one of the returned values from the output from screen -r.

like image 31
Pascal Thivent Avatar answered Jan 24 '23 08:01

Pascal Thivent