linux中的后台进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9430559/
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
Background process in linux
提问by user861973
I have developed a Java socket server connection which is working fine.
我开发了一个运行良好的 Java 套接字服务器连接。
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.
即使用户从启动 jar 文件的位置关闭了终端,我也需要继续。
How can I run Java server socket application in Linux as background process?
如何在 Linux 中运行 Java 服务器套接字应用程序作为后台进程?
采纳答案by ?ukasz R?anek
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.- Use a shell wrapper and the above OR daemonapp. This approach is used by many open source projects and it's quite good for most of the scenarios. Additionally it can be included in
init.d
and required run level with regular start, stop and status commands. I can provide an example if needed. - Build your own daemon server using either Java Service Wrapperor Apache Jakarta Commons Daemon. Again - both are extremely popular, well tested and reliable. And available for both Linux and Windows! The one from Apache Commons is used by Tomcat server! Additionally there is Akuma.
nohup java -server myApplication.jar > /log.txt
- 这很简单。它只会将应用程序置于后台。这会起作用,但这不是一个很好的方法。- 使用 shell 包装器和上面的 OR守护程序应用程序。许多开源项目都使用这种方法,并且对于大多数场景都非常有用。此外,它可以包含在
init.d
所需的运行级别中,并具有常规启动、停止和状态命令。如果需要,我可以提供一个例子。 - 使用Java Service Wrapper或Apache Jakarta Commons Daemon构建您自己的守护程序服务器。再次 - 两者都非常受欢迎,经过良好测试且可靠。并且适用于 Linux 和 Windows!Tomcat 服务器使用 Apache Commons 中的一个!此外还有Akuma。
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.
如果您将来需要使用此服务器和/或将其分发给客户端、最终用户等,我个人会选择解决方案 2 或 3 nohup
。问题。
Ad 2:
广告 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.
对于 Debian/Ubuntu,可以使用基于start-stop-daemon
. 如果有疑问,/etc/init.d/skeleton
可以修改。
#!/bin/sh
DESC="Description"
NAME=YOUR_NAME
PIDFILE=/var/run/$NAME.pid
RUN_AS=USER_TO_RUN
COMMAND=/usr/bin/java -- -jar YOUR_JAR
d_start() {
start-stop-daemon --start --quiet --background --make-pidfile --pidfile $PIDFILE --chuid $RUN_AS --exec $COMMAND
}
d_stop() {
start-stop-daemon --stop --quiet --pidfile $PIDFILE
if [ -e $PIDFILE ]
then rm $PIDFILE
fi
}
case in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 1
d_start
echo "."
;;
*)
echo "usage: $NAME {start|stop|restart}"
exit 1
;;
esac
exit 0
回答by Ata S.
Did you try putting & at the end of the command line? For example:
您是否尝试将 & 放在命令行的末尾?例如:
java -jar mySocketApp.jar &
You can also use bgand fgcommands to send a process to background and foreground. You can pause the running process by CTRL+Z.
您还可以使用bg和fg命令将进程发送到后台和前台。您可以通过CTRL+Z暂停正在运行的进程。
Check it out this article: http://lowfatlinux.com/linux-processes.html
回答by Nirmal- thInk beYond
try,
尝试,
java -jar yourApp.jar &
&
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 &
&
将启动新的进程线程,我还没有测试过这个,但如果它仍然不起作用,那么在脚本文件中twite它并开始我 &
回答by skyronic
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 run disown
after running the java command.
&
在命令末尾添加 a 后,您需要做一件至关重要的事情。该过程仍然链接到终端。disown
运行java命令后需要运行。
java -jar yourApp.jar > log.txt &
disown
Now, you can close the terminal.
现在,您可以关闭终端。
回答by kittylyst
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.
您在这里需要的关键词是“守护进程”。有没有想过为什么系统服务器进程在 Linux / Unix 上经常以“d”结尾?出于历史原因,“d”代表“守护进程”。
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.
它是完全通用的,不仅限于 Java 进程。
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:
您需要担心的两个主要事情是:
- Detach from parent process
- Detach from the tty that created the process
- 从父进程分离
- 从创建进程的 tty 分离
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.
大多数人只会使用一个小的 shell 脚本来启动 java 进程,然后用“&”结束 java 命令以在后台模式下启动。然后,当启动脚本进程退出时,java 进程仍在运行,并将与现在死掉的脚本进程分离。
回答by SuperNova
Step 1.
第1步。
To create new screen
创建新屏幕
screen -RD screenname
Step 2.
第2步。
To enter into screen terminal
进入屏幕终端
press Enter
press Enter
Step 3.
第 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
ctrl + A + D
Step 5.
第 5 步。
To list screen terminals
列出屏幕终端
screen -ls
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.
There is a screen on:
994.screenname (12/10/2018 09:24:31 AM) (Detached)
1 Socket in /run/screen/S-contact.
Step 6.
第 6 步。
To login to the background process
登录后台进程
screen -rd 994.screenname