Linux下的后台进程
我开发了一个 Java 套接字服务器连接,工作正常。
当从终端启动时,它从客户端监听开始。但是当我关闭终端时它会停止监听。
即使用户从启动 jar 文件的位置关闭了终端,我也需要继续。
如何在 Linux 中作为后台进程运行 Java 服务器套接字应用程序?
I have developed a Java socket server connection which is working fine.
When started from a terminal, it starts from listening from client. But when I close the terminal it stops listening.
I need to continue even though the terminal closed by user from where jar file was started.
How can I run Java server socket application in Linux as background process?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
有几种方法可以实现这样的目标:
init.d
中,并包含常规启动、停止和状态命令所需的运行级别。如果需要的话我可以提供一个例子。就我个人而言,如果您将来需要使用此服务器和/或将其分发给客户端、最终用户等,我会选择解决方案 2 或 3。如果您需要运行某些内容并拥有没有时间为问题制定更复杂的解决方案。
广告 2:
可以在此处找到许多项目使用的最佳脚本。
对于 Debian/Ubuntu,可以使用基于
start-stop-daemon
的非常简单的脚本。如果有疑问,可以修改/etc/init.d/sculpture
。There are several ways you can achieve such a thing:
nohup java -server myApplication.jar > /log.txt
- this is pretty straight forward. It will just put the application in the background. This will work but it's just not a very good way to do so.init.d
and required run level with regular start, stop and status commands. I can provide an example if needed.Personally I would go with solution 2 or 3 if you need to use this server in the future and/or distribute it to clients, end users, etc.
nohup
is good if you need to run something and have no time to develop more complex solution for the problem.Ad 2:
The best scripts, used by many projects, can be found here.
For Debian/Ubuntu one can use a very simple script based on
start-stop-daemon
. If in doubt there is/etc/init.d/skeleton
one can modify.在命令末尾添加
&
后,您需要做一件至关重要的事情。该进程仍然链接到终端。运行java命令后需要运行disown
。现在,您可以关闭终端。
There's one crucial thing you need to do after adding a
&
at the end of the command. The process is still linked to the terminal. You need to rundisown
after running the java command.Now, you can close the terminal.
这里需要的关键词是“守护进程”。有没有想过为什么 Linux / Unix 上的系统服务器进程通常以“d”结尾?由于历史原因,“d”代表“守护进程”。
因此,分离并成为真正的服务器进程的过程称为“守护进程”。
它是完全通用的,不仅仅限于 Java 进程。
为了成为真正独立的守护进程,您需要执行几项任务。它们列在维基百科页面上。
您需要担心的两件主要事情是:
如果您在 google 中搜索短语“守护进程”,您将找到许多实现此目的的方法,以及更多详细信息至于为什么有必要。
大多数人只会使用一些 shell 脚本来启动 java 进程,然后用“&”结束 java 命令以后台模式启动。然后,当启动脚本进程退出时,java进程仍在运行,并将与现已死亡的脚本进程分离。
The key phrase you need here is "daemonizing a process". Ever wondered why system server processes often end in 'd' on Linux / Unix? The 'd' stands for "daemon", for historical reasons.
So, the process of detaching and becoming a true server process is called "daemonization".
It's completely general, and not limited to just Java processes.
There are several tasks that you need to do in order to become a truly independent daemon process. They're listed on the Wikipedia page.
The two main things you need to worry about are:
If you google the phrase "daemonizing a process", you'll find a bunch of ways to accomplish this, and some more detail as to why it's necessary.
Most people would just use a little shell script to start up the java process, and then finish the java command with an '&' to start up in background mode. Then, when the startup script process exits, the java process is still running and will be detached from the now-dead script process.
尝试,
&
将启动新的进程线程,我还没有对此进行测试,但如果仍然不起作用,则将其添加到脚本文件中并使用&
启动我try,
&
will start new process thread,I have not tested this, but if still it not works then twite it in script file and start i with&
你有没有尝试把&在命令行末尾?
例如:
您还可以使用bg和fg命令将进程发送到后台和前台。您可以通过CTRL+Z暂停正在运行的进程。
查看这篇文章:http://lowfatlinux.com/linux-processes.html
Did you try putting & at the end of the command line?
For example:
You can also use bg and fg commands to send a process to background and foreground. You can pause the running process by CTRL+Z.
Check it out this article: http://lowfatlinux.com/linux-processes.html
第 1 步。
创建新屏幕
第 2 步。
进入屏幕终端
按 Enter
第 3 步。
运行您的在新打开的终端中运行命令或脚本(在后台运行)
第 4 步。
要退出屏幕终端
ctrl + A + D
第 5 步。< /strong>
列出
将打印如下内容的
屏幕终端
有屏幕上:
994.screenname (12/10/2018 09:24:31 AM)(分离)
/run/screen/S-contact 中的 1 个套接字。
第 6 步.
登录后台进程
Step 1.
To create new screen
Step 2.
To enter into screen terminal
press Enter
Step 3.
Run your command or script (to run in the background) in the newly opened terminal
Step 4.
To come out of screen terminal
ctrl + A + D
Step 5.
To list screen terminals
that will print something like below
There is a screen on:
994.screenname (12/10/2018 09:24:31 AM) (Detached)
1 Socket in /run/screen/S-contact.
Step 6.
To login to the background process
对于相当终端且此过程仍在工作的背景。对我来说,在后台运行该进程的简单快速的方法是在命令末尾使用
&!
:for quite terminal and this process still working background. for me, the simple and fast way to run the process in the background is using the
&!
at end of the command: