arnoldbird
arnoldbird

Reputation: 954

How to create solr service for starting solr on reboot

I am trying to create a solr service script that I can use to start solr automatically on reboot. Here is a script I saw recommended:

#!/bin/sh

# Starts, stops, and restarts Apache Solr.
#
# chkconfig: 35 92 08
# description: Starts and stops Apache Solr

SOLR_DIR="/var/www/html/fas/solr/solr-latest"
JAVA_OPTIONS="-Xmx1024m -DSTOP.PORT=8983 -DSTOP.KEY=mustard -jar /var/www/html/fas/solr/solr-latest/server/start.jar"
LOG_FILE="/var/log/solr.log"
JAVA="/bin/java"

case $1 in
    start)
        echo "Starting Solr"
        cd $SOLR_DIR
        $JAVA $JAVA_OPTIONS 2> $LOG_FILE &
        ;;
    stop)
        echo "Stopping Solr"
        cd $SOLR_DIR
        $JAVA $JAVA_OPTIONS --stop
        ;;
    restart)
        $0 stop
        sleep 1
        $0 start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}" >&2
        exit 1
        ;;
esac

I think I have set the appropriate values for the variables in the script. But when I try to run the script, I get "Connection refused."

$ service solr stop
Stopping Solr
java.net.ConnectException: Connection refused (Connection refused)

I get the same result whether I run the script as root or not.

I can stop and start solr this way, though:

/path/to/my/solr/bin/solr start

So I also tried creating this script at /etc/init.d/solr-start

#!/bin/sh

# Starts Apache Solr.
#
# chkconfig: 35 92 08
# description: Starts Apache Solr

/var/www/html/fas/solr/solr-latest/bin/solr start

This script works at the command line, but it doesn't work on reboot. To try to make it run on reboot, I did...

sudo systemctl enable solr-start

But solr is not started on reboot.

My versions: RHEL 7, Solr 6.6.6

Upvotes: 0

Views: 5041

Answers (2)

ahrooran
ahrooran

Reputation: 1116

For me following worked. I had to add time limits with @rsc 's answer

Solr takes a while to start. If you execute start script, it will say something like

Waiting up to 180 seconds to see Solr running on port 8983 [|]

If you do not add time limits to service, it will immediately check if sub process created / not and kill it using SIGTERM. Then try to restart again and again without waiting, and eventually give up.

Following is my start script

#!/bin/bash

# clear logs
rm -rf /home/company/tools/solr/meta/solr-8.11.4/server/logs/*

# Get the directory of the script
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"

# start
"$SCRIPT_DIR/solr" start -cloud -p 8983 -noprompt

Following is my kill script

#!/bin/bash

# Get the directory of the script
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"

# Navigate to the Solr bin directory
cd "$SCRIPT_DIR" || exit 1

# Stop Solr
./solr stop -p 8983

exit 0

And following is my service file

[Unit]
Description=MetaSolr_DEV
Before=multi-user.target
Conflicts=shutdown.target
After=syslog.target network.target remote-fs.target nss-lookup.target systemd-journald-dev-log.socket

[Service]
Type=forking
User=company
Group=company
Restart=on-failure
RestartSec=5
TimeoutStartSec=60
TimeoutStopSec=30
ExecStart=/home/company/tools/solr/meta/solr-8.11.4/bin/run.sh
ExecStop=/home/company/tools/solr/meta/solr-8.11.4/bin/kill.sh
PIDFile=/home/company/tools/solr/meta/solr-8.11.4/bin/solr-8983.pid

[Install]
WantedBy=multi-user.target

Note the following 3 fields. Adjust them as per your requirements. If solr has ton of configs, then you may need to have higher timeouts

  1. RestartSec=5
  2. TimeoutStartSec=60
  3. TimeoutStopSec=30

Upvotes: 0

rsc
rsc

Reputation: 429

Unfortunately you're providing nearly no details about your specific Solr installation. The following systemd unit example might provide a starting point though.

Create the file /etc/systemd/system/solr.service with the following content (and perform adaptions to make it suitable to your Solr installation):

[Unit]
Description=Apache SOLR
After=syslog.target network.target remote-fs.target nss-lookup.target systemd-journald-dev-log.socket
Before=multi-user.target
Conflicts=shutdown.target

[Service]
User=solr
# Assumes SOLR_PID_DIR; change port if it differs
PIDFile=/var/lib/solr/solr-8983.pid
# Assumes proper configuration in /etc/default/solr.in.sh
Environment=SOLR_INCLUDE=/etc/default/solr.in.sh
ExecStart=/path/to/my/solr/bin/solr start
ExecStop=/path/to/my/solr/bin/solr stop
Restart=on-failure

[Install]
WantedBy=multi-user.target

Perform the following commands either as root or prefix them with sudo:

  • systemctl daemon-reload
  • systemctl enable solr.service
  • systemctl start solr.service
  • systemctl status solr.service

If you need different (or more) options in the systemd unit, this GitHub Gist was suggested to upstream as possible starting point for upstream inclusion.

Upvotes: 5

Related Questions