I need to run an java program even the terminal is closed.... in server....
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With