Skip to content

Instantly share code, notes, and snippets.

@Daniel-KM
Forked from gadelkareem/solr.service
Last active April 10, 2024 13:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daniel-KM/1fb475a47340d7945fa6c47c945707d0 to your computer and use it in GitHub Desktop.
Save Daniel-KM/1fb475a47340d7945fa6c47c945707d0 to your computer and use it in GitHub Desktop.
systemd service file for Apache SOLR
# put this file in /etc/systemd/system/ as root
# below paths assume solr installed in /opt/solr, SOLR_PID_DIR is /data
# and that all configuration exists in /etc/default/solr.in.sh which is the case if previously installed as an init.d service
# change port in pid file if differs
# note that it is configured to auto restart solr if it fails (Restart=on-faliure) and that's the motivation indeed :)
# to switch from systemv (init.d) to systemd, do the following after creating this file:
# sudo systemctl daemon-reload
# sudo service solr stop # if already running
# sudo systemctl enable solr
# systemctl start solr
# this was inspired by https://confluence.t5.fi/display/~stefan.roos/2015/04/01/Creating+systemd+unit+(service)+for+Apache+Solr
[Unit]
Description=Apache SOLR
ConditionPathExists=/opt/solr
After=syslog.target network.target remote-fs.target nss-lookup.target systemd-journald-dev-log.socket
Before=multi-user.target
Conflicts=shutdown.target
StartLimitIntervalSec=60
[Service]
User=solr
LimitNOFILE=1048576
LimitNPROC=1048576
PIDFile=/var/solr/solr-8983.pid
Environment=SOLR_INCLUDE=/etc/default/solr.in.sh
Environment=RUNAS=solr
Environment=SOLR_INSTALL_DIR=/opt/solr
ExecStart=/opt/solr/bin/solr start
ExecStop=/opt/solr/bin/solr stop
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
@igorbga
Copy link

igorbga commented Aug 8, 2023

You have Restart line repeated twice, line 29 and line 34. Is this on-purpose?

@neufeind
Copy link

neufeind commented Apr 5, 2024

I wonder if you're missing a "Type=forking" here, since the script immediately exits/forks and solr itself lives as a child. Thus systemd can't find the pid of the real solr-process. Since you're using the PIDFile-parameter that seems to work still. One interesting thing is that you need the "User"-parameter you have in your unit here since otherwise it would be started as root, the runas would switch down to the desired user but systemd would complain about the PIDFile having a non-root and the wrong owner.

Another interesting idea I would was executing solr itself in foreground, so the process is a child of systemd and systemd knows the pid, has a direct connection, can take care of the logging etc. (start-parameter -f). See:
https://gist.github.com/alanorth/2c3f0a0d7a7036bd986cad5ba371f374

Meanwhile the latest solr-sources also ship a service-definition themselves, but with Type=forking and without the foreground-parameter.
https://github.com/apache/solr/blob/main/solr/bin/systemd/solr.service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment