在 Linux 上运行 Java 应用程序即服务 [英] Run a Java Application as a Service on Linux
问题描述
我编写了一个在标准虚拟托管 Linux 解决方案上运行的 Java 服务器应用程序.应用程序一直在运行,侦听套接字连接并为它们创建新的处理程序.它是客户端-服务器应用程序的服务器端实现.
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.
我启动它的方式是将它包含在服务器的启动 rc.local 脚本中.然而,一旦启动,我不知道如何访问它来停止它,如果我想安装更新,所以我必须重新启动服务器才能重新启动应用程序.
The way I start it is by including it in the start up rc.local script 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.
在 Windows PC 上,对于这种类型的应用程序,我可能会创建一个 Windows 服务,然后我可以根据需要停止和启动它.在 Linux 机器上是否有类似的东西,以便如果我启动这个应用程序,我可以停止它并重新启动它,而无需完全重新启动服务器.
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.
我的应用程序名为 WebServer.exe.它在服务器启动时通过将其包含在我的 rc.local 中来启动:
My application is called WebServer.exe. It is started on server startup by including it in my rc.local as such:
java -jar /var/www/vhosts/myweb.com/phpserv/WebServer.jar &
我在 Linux 上有点菜鸟,所以任何例子都会被任何帖子所欣赏.但是,我确实有 SSH 和完全 FTP 访问权限来安装任何更新以及访问 Plesk 面板.
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.
推荐答案
我在这里写了另一个简单的包装器:
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 $1 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
您可以在 此处 了解 init.d 的完整教程和对于 systemd (ubuntu 16+)这里
You can follow a full tutorial for init.d here and for systemd (ubuntu 16+) here
如果需要输出日志替换2
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
行
nohup java -jar $PATH_TO_JAR >> myService.out 2>&1&
这篇关于在 Linux 上运行 Java 应用程序即服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!