126

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

4 Answers 4

307

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.

4
  • 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, 2012 at 5: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. Aug 27, 2012 at 0: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. Dec 14, 2018 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? Feb 19, 2020 at 6:16
70

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
4
  • This is the modern way of doing it
    – aardbol
    May 30, 2019 at 18:21
  • 4
    on aws ubutnu image I had to install systemd then found the image in /etc/systemd/system/
    – Robbo_UK
    Jun 3, 2019 at 13:46
  • Where could I find the logs with this method? Apr 26, 2022 at 6:39
  • If you have created the file, but face issue like "Failed to start ---- service", then run systemctl daemon-reload before Step 4. unix.stackexchange.com/questions/364782/…
    – abitcode
    Aug 15, 2022 at 19:51
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
5
  • 3
    Is that a systemd service? This question is not tagged linux, and very few distros have systemd by default.
    – jordanm
    Aug 24, 2012 at 3:15
  • 1
    I will amend the question to say that this is using "upstart" which is included in ubuntu.
    – Strelok
    Aug 24, 2012 at 5: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 6, 2015 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. Sep 9, 2015 at 16:30
  • Also, starting a service requires "sudo". sudo service yourservice start Sep 9, 2015 at 16:36
19

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

nohup java -jar /web/server.jar > log.log 2>&1 &

Not the answer you're looking for? Browse other questions tagged or ask your own question.