Run java jar file on a server as background process

Bernad Ali picture Bernad Ali · Aug 24, 2012 · Viewed 192.1k times · Source

I need to run a java jar in server in order to communicate between two applications. I have written two shell scripts to run it, but once I start up that script I can't shut down / terminate the process. If I press ctrl+C or close the console, the server will shut down. Could anyone help me how to modify this script to run as a normal server?

 #!/bin/sh
java -jar /web/server.jar
echo $! 
#> startupApp.pid

Answer

Anton Beloglazov picture Anton Beloglazov · Aug 24, 2012

You can try this:

#!/bin/sh
nohup java -jar /web/server.jar &

The & symbol, switches the program to run in the background.

The nohup utility makes the command passed as an argument run in the background even after you log out.