在Linux上运行Java应用程序即服务

编程入门 行业动态 更新时间:2024-10-22 07:32:29
本文介绍了在Linux上运行Java应用程序即服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我编写了一个在标准虚拟托管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/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

If you need the output log replace the 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应用程序即服务

本文发布于:2023-11-15 02:47:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1590462.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   Linux   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!