Linux 如何自动启动 Solr?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2150767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start Solr automatically?
提问by bluedaniel
At the moment I have to go to /usr/java/apache-solr-1.4.0/example
and then do:
目前我必须去/usr/java/apache-solr-1.4.0/example
然后做:
java -jar start.jar
How do I get this to start automatically on boot?
如何让它在启动时自动启动?
I'm on a shared Linux server.
我在共享 Linux 服务器上。
采纳答案by 0xfe
If you have root access to your machine, there are a number of ways to do this based on your system's initialization flow (init scripts, systemd, etc.)
如果您对您的机器有 root 访问权限,则有多种方法可以根据系统的初始化流程(init 脚本、systemd 等)执行此操作
But if you don't have root, cron
has a clean and consistent way to execute programs upon reboot.
但是,如果您没有 root,cron
则可以在重新启动时以干净且一致的方式执行程序。
First, find out where java is located on your machine. The command below will tell you where it is:
首先,找出java在你机器上的位置。下面的命令会告诉你它在哪里:
$ which java
Then, stick the following code into a shell script, replacing the java path below (/usr/bin) with the path you got from the above command.
然后,将以下代码粘贴到 shell 脚本中,将下面的 java 路径 (/usr/bin) 替换为您从上述命令中获得的路径。
#!/bin/bash
cd /usr/java/apache-solr-1.4.0/example
/usr/bin/java -jar start.jar
You can save this script in some location (e.g., $HOME) as start.sh. Give it world execute permission (to simplify) by running the following command:
您可以将此脚本保存在某个位置(例如 $HOME)作为 start.sh。通过运行以下命令为其授予全局执行权限(以简化):
$ chmod og+x start.sh
Now, test the script and ensure that it works correctly from the command line.
现在,测试脚本并确保它从命令行正常工作。
$ ./start.sh
If all works well, you need to add it to one of your machine's startup scripts. The simplest way to do this is to add the following line to the end of /etc/rc.local.
如果一切正常,您需要将它添加到您机器的启动脚本之一。最简单的方法是将以下行添加到/etc/rc.local的末尾。
# ... snip contents of rc.local ...
# Start Solr upon boot.
/home/somedir/start.sh
Alternatively, if you don't have permission to edit rc.local, then you can add it to your user crontab as so. First type the following on the commandline:
或者,如果您没有编辑 rc.local 的权限,那么您可以将其添加到您的用户 crontab 中。首先在命令行输入以下内容:
$ crontab -e
This will bring up an editor. Add the following line to it:
这将打开一个编辑器。向其中添加以下行:
@reboot /home/somedir/start.sh
If your Linux system supports it (which it usually does), this will ensure that your script is run upon startup.
如果您的 Linux 系统支持它(通常支持),这将确保您的脚本在启动时运行。
If I don't have any typos above, it should work out well for you. Let me know how it goes.
如果我上面没有任何错别字,它应该适合你。让我知道事情的后续。
回答by John La Rooy
Check
查看
man 5 crontab
See if @reboot
is supported on the Linux system you are using.
查看@reboot
您使用的 Linux 系统是否支持。
回答by SuperMagic
As you're on a shared Linux box, you'll have to ask the system administrator to do the following, probably.
当您在共享 Linux 机器上时,您可能必须要求系统管理员执行以下操作。
Create a startup script in /etc/init.d/solr
.
中创建启动脚本/etc/init.d/solr
。
Copy this code, my Solr startup script, into that file:
将此代码(我的 Solr 启动脚本)复制到该文件中:
#!/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 "" in
start)
start
;;
status)
status
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: solr {start|status|stop|restart}"
exit 3
;;
esac
exit $RETVAL
Then run:
然后运行:
chkconfig --add solr
chkconfig --add solr
Or (on Ubuntu):
或者(在 Ubuntu 上):
update-rc.d solr defaults
更新 rc.d solr 默认值
... or, if your Linux distribution doesn't have chkconfig
(or update-rc.d
), link /etc/init.d/solr
to /etc/rc3.d/S99solr
and /etc/rc5.d/S99solr
and /etc/rc3.d/K01solr
and /etc/rc5.d/K01solr
:
... 或者,如果您的 Linux 发行版没有chkconfig
(或update-rc.d
),则链接/etc/init.d/solr
到/etc/rc3.d/S99solr
and/etc/rc5.d/S99solr
和/etc/rc3.d/K01solr
and /etc/rc5.d/K01solr
:
% ln -s /etc/init.d/solr /etc/rc3.d/S99solr
% ln -s /etc/init.d/solr /etc/rc5.d/S99solr
% ln -s /etc/init.d/solr /etc/rc3.d/K01solr
% ln -s /etc/init.d/solr /etc/rc5.d/K01solr
Now on reboot Solr will startup in run levels 3 and 5 (console with network & full GUI).
现在重新启动 Solr 将在运行级别 3 和 5 中启动(带有网络和完整GUI 的控制台)。
To start solr manually run:
要手动启动 solr,请运行:
% /etc/init.d/solr start
回答by Visitor
Follow supermagic's comments, then follow this
按照supermagic的评论,然后按照这个
http://codingrecipes.com/service-x-does-not-support-chkconfig
http://codingrecipes.com/service-x-does-not-support-chkconfig
He says,
他说,
1 – Copy your script into /etc/init.d folder 2 – cd /etc/init.d 3 – chmod +x myscript 4 – Add these lines, including #, right after #!/bin/bash or #!/bin/sh: # chkconfig: 2345 95 20 # description: Some description # What your script does (not sure if this is necessary though) # processname: myscript
Then you can do
然后你可以做
chkconfig --add myscript
chkconfig --add myscript
回答by Daniel Rijkhof
Adding the following lines to my /etc/init.d/solr
file works to support Red Hat Linux (I copied them from /etc/init.d/mysql
after reading comments by others here).
将以下行添加到我的/etc/init.d/solr
文件中以支持 Red Hat Linux(我/etc/init.d/mysql
在阅读其他人的评论后复制了它们)。
# Comments to support chkconfig on Red Hat Linux
# chkconfig: 2345 64 36
# Description: A very fast and reliable search engine.
回答by mlissner
There are three steps that you need to do here:
您需要在此处执行三个步骤:
- Create the script, make it executable, and put it in the right place.
- Make the script start up properly on reboot.
- Bonus: Set up a logrotation script so logs don't get out of control.
- 创建脚本,使其可执行,并将其放在正确的位置。
- 使脚本在重新启动时正确启动。
- 奖励:设置 logrotation 脚本,以便日志不会失控。
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.
首先,我已经从上面调整了 supermagic 的脚本。没关系,但是有一些拼写错误,缺少一些功能(状态,重新启动),没有非常有效地使用守护程序实用程序。
Here's my version of the script (make sure you have daemoninstalled 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 "" 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
将此脚本放在 /etc/init.d/solr 中,使其可执行,您应该能顺利完成第一步。您现在可以使用 /etc/init.d/solr start|stop|restart|status 启动/停止/重新启动/状态 solr 守护进程
For step two, run the following on an Ubuntu machine (don't know about Redhat):
对于第二步,在 Ubuntu 机器上运行以下命令(不了解 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).
将该文件放在 /etc/logrotate.d/solr 中,您应该很高兴,假设 logrotate 正在运行(通常是这样)。
回答by mlissner
I answered this question once already, but that answer was for operating systems that used SysV and this one is for newer operating systems that are increasingly using systemd.
我曾经回答过这个问题,但那个答案是针对使用 SysV 的操作系统的,而这个答案是针对越来越多地使用systemd 的较新的操作系统。
As in my other answer, there are three things you'll need to do here:
和我的其他回答一样,您需要在这里做三件事:
- Create the script and put it in the right place.
- Make the script start up properly on reboot.
- Bonus: Make
systemd
logs persistent.
- 创建脚本并将其放在正确的位置。
- 使脚本在重新启动时正确启动。
- 奖励:使
systemd
日志持久化。
1. Creating the script and putting it in the right place
1.创建脚本并将其放在正确的位置
Here's a systemd
unit file that you can use (these replace the SysV init files). Name it solr.service
.
这systemd
是您可以使用的单元文件(这些替换了 SysV init 文件)。命名它solr.service
。
[Unit]
Description=Apache Solr
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=simple
Environment="XMX=2G"
WorkingDirectory=/usr/local/solr/example
ExecStart=/usr/bin/java -jar -server -Xmx${XMX} start.jar
Restart=on-failure
LimitNOFILE=10000
[Install]
WantedBy=multi-user.target
Note that there's a configuration in there for Solr's memory. You'll probably want to tweak that to your own purposes. If you have a number of variables you're passing to systemd
, you can do that with the EnvironmentFile
directive.
请注意,那里有一个用于 Solr 内存的配置。您可能希望根据自己的目的对其进行调整。如果您有许多要传递给的变量systemd
,则可以使用该EnvironmentFile
指令执行此操作。
More documentation for these files is here.
2. Make the script startup properly at boot
2. 使脚本在开机时正常启动
This is fairly simple, but there are rules. To make it start at boot, put the file in /etc/systemd/system/solr.service
. You cannot use a symlink in this directory, do not try.
这很简单,但有规则。要使其在启动时启动,请将文件放入/etc/systemd/system/solr.service
. 您不能在此目录中使用符号链接,请勿尝试.
Once that's in there, you can enable the daemon to run at boot with:
一旦它在那里,你可以启用守护进程在启动时运行:
sudo systemctl enable solr
And you can start, stop, status it with:
您可以使用以下命令启动、停止、设置状态:
sudo systemctl {start|stop|status} solr
3. Making systemd
logs persistent
3. 使systemd
日志持久化
By default, systemd
logs are not persistent and they are lost whenever you reboot the system. If that's not what you desire, you can make them persistent by creating a directory:
默认情况下,systemd
日志不是持久的,只要您重新启动系统,它们就会丢失。如果这不是你想要的,你可以通过创建一个目录来使它们持久化:
sudo mkdir -p /var/log/journal/
And then restarting the systemd
journaling daemon:
然后重新启动systemd
日志守护进程:
sudo systemctl restart systemd-journald
Once that's complete, systemd
's journaling daemonwill receive all the stdout and stderr that Solr creates and it will get stored in a binary formatunder /var/log/journal/
.
一旦这完成后,systemd
的日志守护进程将接收所有的输出和错误是Solr的创建,它会获取存储在二进制格式下/var/log/journal/
。
The way systemd
handles logging is pretty neat and is worth studying if you're not familiar with it. In the meantime, just know that to read your log entries you'll need to use a new tool called journalctl
. For example, this will follow your solr logs:
systemd
处理日志的方式非常简洁,如果您不熟悉它,值得研究。与此同时,只要知道要读取您的日志条目,您需要使用一个名为journalctl
. 例如,这将遵循您的 solr 日志:
journalctl -u solr -f
And there are also flags for doing date-based filtering and things like that.
还有一些标志用于进行基于日期的过滤之类的事情。
3.1 Tweaking the log files
3.1 调整日志文件
Bonus section! If you want to tweak the log files, you can read all about it in the documentation here, but the defaults are actually very safe and sane (logs are compressed by default, can't grow too large, are rate limited, and are written to disk in batches).
奖金部分!如果你想调整日志文件,你可以在此处的文档中阅读所有相关信息,但默认值实际上非常安全和合理(默认情况下日志被压缩,不能增长太大,速率受限,并且被写入批量磁盘)。
回答by Mayank Jaiswal
init.d/solr script that's tested on Centos 6/Amazon Linux. It wraps solr's native CLI.
在 Centos 6/Amazon Linux 上测试过的 init.d/solr 脚本。它包装了 solr 的原生 CLI。
#!/bin/bash
# description: Starts and stops Solr production
PIDFILE=/var/run/solr.pid
SOLR_HOME=/usr/share/solr
START_COMMAND="$SOLR_HOME/bin/solr start -p 8984 -noprompt -m 1g"
NAME="Solr"
start() {
echo "Starting $NAME"
if [ -f $PIDFILE ]; then
echo -n "$PIDFILE exists. $NAME may be running."
else
str=`$START_COMMAND`
pid=`echo $str | grep -o "pid=[0-9]*" | grep -o "[0-9]*"`
if [ "$pid" == "" ];
then
echo "[FATAL ERROR] Failed to extract pid. Exiting!"
exit 1
fi
echo $pid > $PIDFILE
fi
return 0
}
case "" in
start)
start
;;
stop)
echo "Stopping $NAME .."
$SOLR_HOME/bin/solr stop
rm -f $PIDFILE
;;
status)
$SOLR_HOME/bin/solr status
;;
restart)
##代码## stop
#sleep 2
##代码## start
;;
*)
echo "Usage: ##代码## (start | stop | restart | status)"
exit 1
;;
esac
exit $?