Run java jar file on a server as background process
Asked Answered
J

4

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
Jujutsu answered 24/8, 2012 at 1:49 Comment(0)
P
320

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.

Parricide answered 24/8, 2012 at 11:34 Comment(4)
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?Jujutsu
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.Parricide
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.Genius
@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?Peripatetic
A
75

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
Amphibolite answered 24/2, 2015 at 19:16 Comment(4)
This is the modern way of doing itOdontoid
on aws ubutnu image I had to install systemd then found the image in /etc/systemd/system/Oscillator
Where could I find the logs with this method?Tessin
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/…Steakhouse
F
34

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
Formalin answered 24/8, 2012 at 2:41 Comment(5)
Is that a systemd service? This question is not tagged linux, and very few distros have systemd by default.Hoodlum
I will amend the question to say that this is using "upstart" which is included in ubuntu.Formalin
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 &Czech
All system jobs by default live in the following directory: /etc/init/ ... By default, there is no /var/init/ directory in Ubuntu 14.04.Broken
Also, starting a service requires "sudo". sudo service yourservice startBroken
C
23

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

nohup java -jar /web/server.jar > log.log 2>&1 &
Canula answered 18/6, 2020 at 5:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.