在linux中启动类似service的jar文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21546278/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:57:02  来源:igfitidea点击:

Start a jar file like service in linux

javalinuxshelljarexecutable-jar

提问by Alex Mathew

I want to start and stop my jar file as follows

我想按如下方式启动和停止我的 jar 文件

service myService start

service myService start

service myService stop

service myService stop

my current jar file running as follows

我当前的 jar 文件运行如下

cd /home/alex/IdeaProjects/myService
java -jar target/myService-SNAPSHOT-1.jar server config.yml

What should I do?

我该怎么办?

采纳答案by PbxMan

You need a Service Wrapper to run the Jar file.

您需要一个服务包装器来运行 Jar 文件。

There are examples and instructions for init.d here.or for systemd (ubuntu 16+) here

这里有 init.d 的示例和说明或者对于 systemd (ubuntu 16+)在这里

回答by Jorge_B

If you want to try the DIY way, you can place a startup script in your /etc/init.d directory as said here.

如果你想尝试 DIY 方式,你可以在你的 /etc/init.d 目录中放置一个启动脚本,如这里所说。

http://www.ghacks.net/2009/04/04/get-to-know-linux-the-etcinitd-directory/

http://www.ghacks.net/2009/04/04/get-to-know-linux-the-etcinitd-directory/

However, to implement a clean yourScript stopcommand, I would recommend that you split your functionality into a launcher and a daemon, and make your launcher able to start or communicate with your existing daemon in order to send orders to it. Then your startup script would only invoke your launcher, which in its turn would start a new daemon, or send orders to the existing one.

但是,要实现一个干净的yourScript stop命令,我建议您将您的功能拆分为一个启动器和一个守护程序,并使您的启动器能够启动或与您现有的守护程序通信,以便向它发送命令。然后你的启动脚本只会调用你的启动器,它反过来会启动一个新的守护进程,或者向现有的守护进程发送命令。