127

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
Lefty G Balogh
  • 1,771
  • 3
  • 26
  • 40
Bernad Ali
  • 1,699
  • 6
  • 23
  • 29

4 Answers4

313

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.

Anton Beloglazov
  • 4,939
  • 1
  • 21
  • 9
  • 1
    Thanks Anton,currntly im stopping the server by killing the process id.i don't think it's best practice.is thery command to to stop the server? – Bernad Ali Aug 25 '12 at 05:01
  • 3
    Short answer: it depends on the server. Long answer: as far as I know, there is no safe way to shut down a process without the process supporting such graceful shut downs. For example, if it's a web server being terminated by an external signal, there is always a possibility that some requests will be lost. One way to solve this problem is to implement a graceful termination function in the server itself, e.g. by processing a special kind of requests. Then, the server can be terminated by sending it a request of that special kind. Otherwise, killing the process by its ID is the simple way. – Anton Beloglazov Aug 27 '12 at 00:33
  • 1
    Using `nohup` should always be combined with redirecting stdout and stderr explicitly -- otherwise, you don't get control of where the logs go, and end up with an ugly `nohup.out` created in whichever directory this script happens to be invoked from. – Charles Duffy Dec 14 '18 at 16:23
  • @Anton when I run a script(just like your answer) with SSH EXEC runjar.sh. It started streaming the logs in my local terminal and if close my terminal my jar gets killed. How can I run a jar in the background using SSH EXEC? – AATHITH RAJENDRAN Feb 19 '20 at 06:16
73

Systemd which now runs in the majority of distros

Step 1:

Find your user defined services mine was at /usr/lib/systemd/system/

Step 2:

Create a text file with your favorite text editor name it whatever_you_want.service

Step 3:

Put following Template to the file whatever_you_want.service

[Unit]
Description=webserver Daemon

[Service]
ExecStart=/usr/bin/java -jar /web/server.jar
User=user

[Install]
WantedBy=multi-user.target

Step 4:

Run your service
as super user

$ systemctl start whatever_you_want.service # starts the service
$ systemctl enable whatever_you_want.service # auto starts the service
$ systemctl disable whatever_you_want.service # stops autostart
$ systemctl stop whatever_you_want.service # stops the service
$ systemctl restart whatever_you_want.service # restarts the service
mtelesha
  • 2,079
  • 18
  • 16
33

If you're using Ubuntu and have "Upstart" (http://upstart.ubuntu.com/) you can try this:

Create /var/init/yourservice.conf

with the following content

description "Your Java Service"  
author "You"  

start on runlevel [3]  
stop on shutdown  

expect fork  

script     
    cd /web 
    java -jar server.jar >/var/log/yourservice.log 2>&1  
    emit yourservice_running  
end script  

Now you can issue the service yourservice start and service yourservice stop commands. You can tail /var/log/yourservice.log to verify that it's working.

If you just want to run your jar from the console without it hogging the console window, you can just do:

java -jar /web/server.jar > /var/log/yourservice.log 2>&1
Serg
  • 2,346
  • 3
  • 29
  • 38
Strelok
  • 50,229
  • 9
  • 102
  • 115
  • 3
    Is that a systemd service? This question is not tagged linux, and very few distros have systemd by default. – jordanm Aug 24 '12 at 03:15
  • 1
    I will amend the question to say that this is using "upstart" which is included in ubuntu. – Strelok Aug 24 '12 at 05:34
  • 1
    I believe for the last advice if you want it to run in the background and the shell got released, you should do: `java -jar /web/server.jar > /var/log/yourservice.log 2>&1 &` – Montaro May 06 '15 at 22:52
  • All system jobs by default live in the following directory: /etc/init/ ... By default, there is no /var/init/ directory in Ubuntu 14.04. – Michael Lafayette Sep 09 '15 at 16:30
  • Also, starting a service requires "sudo". sudo service yourservice start – Michael Lafayette Sep 09 '15 at 16:36
21

Run in background and add logs to log file using the following:

nohup java -jar /web/server.jar > log.log 2>&1 &
KayV
  • 12,987
  • 11
  • 98
  • 148