5

I am a newbie when it comes to Java and Jetty app deployment. I use the default settings for setting up my jetty serve and ran java -jar start.jar on my terminal window. The server runs as expected, but when I close my terminal it stops. Is this normal? I used XAMPP before and there you can close the terminal without any problem. How do I overcome this problem, everybody needs to shut down there personal computer once in a while.

I'm using a mac btw.

kristianp
  • 5,496
  • 37
  • 56
einstein
  • 13,389
  • 27
  • 80
  • 110

5 Answers5

11

It sounds like you're using ssh or something like that to start Jetty on a remote Linux/Unix server.

So, you can use nohup java -jar start.jar & - nohup will prevent your process from being stopped by the usual Unix "hangup" signal (ref) when you log out, and the & will put jetty as a background process so you can type exit or whatever to log out.

If you want to be able to re-attach to the Jetty terminal, I'd recommend reading up on GNU screen.

If you want to stop jetty gracefully again, I'd really recommend using it as a service, or using screen to avoid losing the terminal. But if it's too late for that you can find the PID in the output of jps -l and then call kill $PID.

Matthew Gilliard
  • 9,298
  • 3
  • 33
  • 48
  • Have you type any character wrong? I don't get it work, the server starts but I can't close my terminal. I'm using SSH btw, in Eclipse RSE terminal. I typed as you said `java -jar start.jar &` – einstein Mar 02 '11 at 21:44
  • with the `&`it runs this error first: 2011-03-02 21:45:10.165:INFO::NO JSP Support for /, did not find org.apache.jasper.servlet.J spServlet 2011-03-02 21:45:10.248:INFO::Logging to StdErrLog::DEBUG=false via org.eclipse.jetty.util.l og.StdErrLog – einstein Mar 02 '11 at 21:46
  • And then I click any key and the server starts as normal – einstein Mar 02 '11 at 21:47
  • Thats not quite corret I guess. If you just & the process it is still bound to your terminal session. You have to decouple the process by the nohup command, like Dmytro mentioned. – onigunn Mar 02 '11 at 21:49
  • try adding the full path of the start.jar. E.g. it is located at /opt/jetty - java -jar /opt/jetty/start.jar & – onigunn Mar 02 '11 at 21:50
  • Yep, sorry. I added "nohup" to my answer. – Matthew Gilliard Mar 02 '11 at 21:53
4

try "nohup java -jar start.jar &"

and i'm already say it in previous question )

Dmytro
  • 195
  • 5
  • Ok it worked now! Thanks really appreciate it. But how do I close it? Not that I need to close my server that much ,but do I run `java -jar start.jar &` and `Ctrl + C`? – einstein Mar 02 '11 at 21:54
1

You can properly install it as a linux service(if you are actually connecting to a linux server) too.

cd to your jetty folder, for example mine is:

cd /home/spydon/jetty/

They have actually made most of the work with the jetty.sh file, so copy that one to /etc/init.d/

sudo cp ./bin/jetty.sh /etc/init.d/jetty

Then open the file with your favorite text editor, like vim or nano

sudo vim /etc/init.d/jetty

In the beginning simply uncomment(simply remove the hash(#)) three lines that says something like

 #chkconfig: 3 99 99
 #description: Jetty 9 webserver
 #processname: jetty

Meanwhile you have the text editor open, also add the jetty home directory to the beginning of the file, mine looks like this:

#!/usr/bin/env bash  
#
# Startup script for jetty under *nix systems (it works under NT/cygwin too).
JETTY_HOME=/home/spydon/jetty

# To get the service to restart correctly on reboot, uncomment below (3 lines):
# ========================
 chkconfig: 3 99 99
 description: Jetty 9 webserver
 processname: jetty
# ========================

(You don't actually need to uncomment those three lines for it to work, only add the jetty_home. But for a proper deploy you should probably fix those lines.)

Now you should be able to start it with

sudo /etc/init.d/jetty start

And if you want it to run every time you boot, simply add

sudo ln -s /etc/init.d/jetty /etc/rc1.d/K99jetty
sudo ln -s /etc/init.d/jetty /etc/rc2.d/S99jetty

This should work for most modern distros, but I've only tried it on debian based ones.

spydon
  • 9,372
  • 6
  • 33
  • 63
1

You can set up jetty to run as a service... here's the instruction for linux and windows. This way, you don't need to worry about launching jetty everytime through the terminal.

limc
  • 39,366
  • 20
  • 100
  • 145
1

How to kill process:

1) java style

when start jetty :

java -DSTOP.PORT=8077 -DSTOP.KEY=secret_key_only_admin_know -jar start.jar

for stop:

java -DSTOP.PORT=8077 -DSTOP.KEY=secret_key_only_admin_know -jar start.jar -stop

P.S. ports can be any - but they must be the same for start and kill commands :)

2) linux style

kill process by PID

Dmytro
  • 195
  • 5