在 Linux 上运行 Java 应用程序即服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11203483/
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
Run a Java Application as a Service on Linux
提问by dreza
I have written a Java server application that runs on a standard virtual hosted Linux solution. The application runs all the time listening for socket connections and creating new handlers for them. It is a server side implementation to a client-server application.
我编写了一个在标准虚拟托管 Linux 解决方案上运行的 Java 服务器应用程序。应用程序一直在运行,侦听套接字连接并为它们创建新的处理程序。它是客户端-服务器应用程序的服务器端实现。
The way I start it is by including it in the start up rc.localscript of the server. However once started I do not know how to access it to stop it and if I want to install an update, so I have to restart the server in order to restart the application.
我启动它的方式是将它包含在服务器的启动rc.local脚本中。然而,一旦启动,我不知道如何访问它来停止它,如果我想安装更新,所以我必须重新启动服务器才能重新启动应用程序。
On a windows PC, for this type of application I might create a windows service and then I can stop and start it as I want. Is there anything like that on a Linux box so that if I start this application I can stop it and restart it without doing a complete restart of the server.
在 Windows PC 上,对于这种类型的应用程序,我可能会创建一个 Windows 服务,然后我可以根据需要停止和启动它。在 Linux 机器上有没有类似的东西,这样如果我启动这个应用程序,我就可以停止它并重新启动它,而无需完全重新启动服务器。
My application is called WebServer.exe. It is started on server startup by including it in my rc.localas such:
我的应用程序称为 WebServer.exe。它是在服务器启动时通过将其包含在我的rc.local 中来启动的:
java -jar /var/www/vhosts/myweb.com/phpserv/WebServer.jar &
I am a bit of a noob at Linux so any example would be appreciated with any posts. However I do have SSH, and full FTP access to the box to install any updates as well as access to a Plesk panel.
我在 Linux 上有点菜鸟,所以任何例子都会被任何帖子所赞赏。但是,我确实有 SSH 和对盒子的完全 FTP 访问权限以安装任何更新以及访问 Plesk 面板。
采纳答案by PbxMan
I wrote another simple wrapper here:
我在这里写了另一个简单的包装器:
#!/bin/sh
SERVICE_NAME=MyService
PATH_TO_JAR=/usr/local/MyProject/MyJar.jar
PID_PATH_NAME=/tmp/MyService-pid
case in
start)
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_NAME ]; then
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is already running ..."
fi
;;
stop)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stoping ..."
kill $PID;
echo "$SERVICE_NAME stopped ..."
rm $PID_PATH_NAME
else
echo "$SERVICE_NAME is not running ..."
fi
;;
restart)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stopping ...";
kill $PID;
echo "$SERVICE_NAME stopped ...";
rm $PID_PATH_NAME
echo "$SERVICE_NAME starting ..."
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is not running ..."
fi
;;
esac
You can follow a full tutorial for init.d hereand for systemd (ubuntu 16+)here
您可以按照一个完整的教程的init.d这里和systemd(Ubuntu的16+)这里
If you need the output log replace the 2
如果您需要输出日志替换 2
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
lines for
线为
nohup java -jar $PATH_TO_JAR >> myService.out 2>&1&
回答by carlspring
Another alternative, which is also quite popular is the Java Service Wrapper. This is also quite popular around the OSS community.
另一种非常流行的替代方法是Java Service Wrapper。这在 OSS 社区中也很流行。
回答by hovanessyan
However once started I don't know how to access it to stop it
但是一旦开始我不知道如何访问它来阻止它
You can write a simple stop script that greps for your java process, extracts the PID and calls kill on it. It's not fancy, but it's straight forward. Something like that may be of help as a start:
您可以编写一个简单的停止脚本,为您的 Java 进程 grep,提取 PID 并对其调用 kill。这并不花哨,但很直接。作为开始,这样的事情可能会有所帮助:
#!/bin/bash
PID = ps ax | grep "name of your app" | cut -d ' ' -f 1
kill $PID
回答by Arcadien
Linux service init script are stored into /etc/init.d
. You can copy and customize /etc/init.d/skeleton
file, and then call
Linux 服务初始化脚本存储在/etc/init.d
. 您可以复制和自定义/etc/init.d/skeleton
文件,然后调用
service [yourservice] start|stop|restart
see http://www.ralfebert.de/blog/java/debian_daemon/. Its for Debian (so, Ubuntu as well) but fit more distribution.
见http://www.ralfebert.de/blog/java/debian_daemon/。它适用于 Debian(因此,也适用于 Ubuntu)但适合更多发行版。
回答by Wernsey
A simple solution is to create a script start.sh that runs Java through nohup and then stores the PID to a file:
一个简单的解决方案是创建一个脚本 start.sh,它通过 nohup 运行 Java,然后将 PID 存储到一个文件中:
nohup java -jar myapplication.jar > log.txt 2> errors.txt < /dev/null &
PID=$!
echo $PID > pid.txt
Then your stop script stop.sh would read the PID from the file and kill the application:
然后您的停止脚本 stop.sh 将从文件中读取 PID 并终止应用程序:
PID=$(cat pid.txt)
kill $PID
Of course I've left out some details, like checking whether the process exists and removing pid.txt
if you're done.
当然,我遗漏了一些细节,比如检查进程是否存在,pid.txt
如果完成则删除。
回答by tienthanhakay
You can use Thrift serveror JMXto communicate with your Java service.
您可以使用Thrift 服务器或JMX与您的 Java 服务进行通信。
回答by Dawngerpony
From Spring Boot application as a Service, I can recommend the Python-based supervisord
application. See that stack overflow question for more information. It's really straightforward to set up.
从Spring Boot application as a Service,我可以推荐基于 Python 的supervisord
应用程序。有关更多信息,请参阅堆栈溢出问题。设置起来非常简单。
回答by Peter Peterson
Maybe not the best dev-ops solution, but good for the general use of a server for a lan party or similar.
也许不是最好的 dev-ops 解决方案,但适用于局域网聚会或类似服务器的一般用途。
Use screen
to run your server in and then detach before logging out, this will keep the process running, you can then re-attach at any point.
使用screen
在运行的服务器,然后注销前分离,这将保持运行的过程中,你就可以在任何时候重新连接。
Workflow:
工作流程:
Start a screen: screen
启动画面: screen
Start your server: java -jar minecraft-server.jar
启动你的服务器: java -jar minecraft-server.jar
Detach by pressing: Ctl-a
, d
按下分离:Ctl-a
,d
Re-attach: screen -r
重新附上: screen -r
More info here: https://www.gnu.org/software/screen/manual/screen.html
更多信息在这里:https: //www.gnu.org/software/screen/manual/screen.html
回答by yglodt
Referring to Spring Boot application as a Serviceas well, I would go for the systemd
version, since it's the easiest, least verbose, and best integrated into modern distros (and even the not-so-modern ones like CentOS 7.x).
也提到Spring Boot 应用程序即服务,我会选择这个systemd
版本,因为它是最简单、最不冗长、并且最好集成到现代发行版(甚至是像 CentOS 7.x 这样不那么现代的发行版)中的版本。
回答by Alan Thompson
The easiest way is to use supervisord
. Please see full details here: http://supervisord.org/
最简单的方法是使用supervisord
. 请在此处查看完整详细信息:http: //supervisord.org/
More info:
更多信息: