How to run Solr Jetty in background

2020-05-20 09:07发布

问题:

I am using the Jetty/Solr build that comes with Solr and would like to run it in the background instead of in the terminal.

Right now I start it by java -jar start.jar but I would like it to log to a file and run in the background on the server so that I can close the terminal window.

I'm sure there is some java config that I can't find.

I have tried java -jar start.jar > log.txt & but no luck still outputs to the terminal window.

Thanks.

回答1:

Try something like:

nohup yourcommand > output.log 2>&1 &

nohup will prevent yourcommand from being terminated in the event you log out.

& will run it in the background.

> output.log will send stdout to output.log

2>&1 will redirect stderr to stdout



回答2:

nohup is used to execute commands that runs after logout from a shell. What you need here is '2>&1'. This redirects standart error to the standart output. So everything will be logged to log.txt. Try this

java -jar start.jar > log.txt 2>&1

Also you can add an '&' start it as a background process.



回答3:

You can run it with screen if you are on unix.



回答4:

You can properly install it as a linux service 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 (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 now 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
# ========================

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 reboot, 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. You could also consider doing a symlink to the jetty.sh so it will be easier to upgrade.



回答5:

You may want to try nohup, as explained in this previous answer.