How to start Solr automatically?

后端 未结 8 614
深忆病人
深忆病人 2020-11-27 10:07

At the moment I have to go to /usr/java/apache-solr-1.4.0/example and then do:

java -jar start.jar

How do I get this to start

8条回答
  •  误落风尘
    2020-11-27 10:41

    There are three steps that you need to do here:

    1. Create the script, make it executable, and put it in the right place.
    2. Make the script start up properly on reboot.
    3. Bonus: Set up a logrotation script so logs don't get out of control.

    For number one, I've tuned supermagic's script from above. It was OK, but had a number of typos, lacked some functionality (status, restart), didn't use the daemon utility very effectively.

    Here's my version of the script (make sure you have daemon installed for it to work):

    #!/bin/sh
    
    # Prerequisites:
    # 1. Solr needs to be installed at /usr/local/solr/example
    # 2. daemon needs to be installed
    # 3. Script needs to be executed by root
    
    # This script will launch Solr in a mode that will automatically respawn if it
    # crashes. Output will be sent to /var/log/solr/solr.log. A pid file will be 
    # created in the standard location.
    
    start () {
        echo -n "Starting solr..."
    
        # start daemon
        daemon --chdir='/usr/local/solr/example' --command "java -jar start.jar" --respawn --output=/var/log/solr/solr.log --name=solr --verbose
    
        RETVAL=$?
        if [ $RETVAL = 0 ]
        then
            echo "done."
        else
            echo "failed. See error code for more information."
        fi
        return $RETVAL
    }
    
    stop () {
        # stop daemon
        echo -n "Stopping solr..."
    
        daemon --stop --name=solr  --verbose
        RETVAL=$?
    
        if [ $RETVAL = 0 ]
        then
            echo "done."
        else
            echo "failed. See error code for more information."
        fi
        return $RETVAL
    }
    
    
    restart () {
        daemon --restart --name=solr  --verbose
    }
    
    
    status () {
        # report on the status of the daemon
        daemon --running --verbose --name=solr
        return $?
    }
    
    
    case "$1" in
        start)
            start
        ;;
        status)
            status
        ;;
        stop)
            stop
        ;;
        restart)
            restart
        ;;
        *)
            echo $"Usage: solr {start|status|stop|restart}"
            exit 3
        ;;
    esac
    
    exit $RETVAL
    

    Place this script at /etc/init.d/solr, make it executable, and you should be good with step one. You can now start/stop/restart/status a solr daemon with /etc/init.d/solr start|stop|restart|status

    For step two, run the following on an Ubuntu machine (don't know about Redhat):

    update-rc.d solr defaults
    

    Once this is done, you're in pretty good shape, but you probably want to rotate the logs properly at some point, so here's a good config for the logs:

    /var/log/solr/*.log {
      weekly
      rotate 12
      compress
      delaycompress
      create 640 root root
      postrotate
        /etc/init.d/solr restart
      endscript
    }
    

    Place that file in /etc/logrotate.d/solr, and you should be good to go, assuming logrotate is running (it usually is).

提交回复
热议问题