在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
Start a jar file like service in linux
提问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
回答by user2684301
I prefer a light weight, free, bash script rather than a more elaborate system that requires licensing.
我更喜欢轻量级、免费的 bash 脚本,而不是需要许可的更复杂的系统。
http://gustavostraube.wordpress.com/2009/11/05/writing-an-init-script-for-a-java-application/
http://gustavostraube.wordpress.com/2009/11/05/writing-an-init-script-for-a-java-application/
Running jar as a Linux service - init.d script gets stuck starting app
将 jar 作为 Linux 服务运行 - init.d 脚本在启动应用程序时卡住
https://askubuntu.com/questions/99232/how-to-make-a-jar-file-run-on-startup-and-when-you-log-out
https://askubuntu.com/questions/99232/how-to-make-a-jar-file-run-on-startup-and-when-you-log-out
回答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 stop
command, 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
命令,我建议您将您的功能拆分为一个启动器和一个守护程序,并使您的启动器能够启动或与您现有的守护程序通信,以便向它发送命令。然后你的启动脚本只会调用你的启动器,它反过来会启动一个新的守护进程,或者向现有的守护进程发送命令。