Linux下的后台进程

发布于 2025-01-08 20:37:04 字数 160 浏览 0 评论 0原文

我开发了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

绝對不後悔。 2025-01-15 20:37:04

有几种方法可以实现这样的目标:

  1. nohup java -server myApplication.jar > /log.txt - 这非常简单。它只会将应用程序置于后台。这会起作用,但这不是一个很好的方法。
  2. 使用 shell 包装器和上述或 守护进程 应用程序。这种方法被许多开源项目使用,并且对于大多数场景来说都非常好。此外,它还可以包含在 init.d 中,并包含常规启动、停止和状态命令所需的运行级别。如果需要的话我可以提供一个例子。
  3. 使用 Java Service WrapperApache Jakarta Commons 守护进程。再说一遍——两者都非常受欢迎,经过充分测试且可靠。并且适用于 Linux 和 Windows! Tomcat服务器使用的是Apache Commons的!此外还有Akuma

就我个人而言,如果您将来需要使用此服务器和/或将其分发给客户端、最终用户等,我会选择解决方案 2 或 3。如果您需要运行某些内容并拥有没有时间为问题制定更复杂的解决方案。

广告 2:

可以在此处找到许多项目使用的最佳脚本。

对于 Debian/Ubuntu,可以使用基于 start-stop-daemon 的非常简单的脚本。如果有疑问,可以修改 /etc/init.d/sculpture

#!/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 $1 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

There are several ways you can achieve such a thing:

  1. 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.
  2. Use a shell wrapper and the above OR daemon app. 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.
  3. Build your own daemon server using either Java Service Wrapper or 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.

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.

#!/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 $1 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
中性美 2025-01-15 20:37:04

在命令末尾添加 & 后,您需要做一件至关重要的事情。该进程仍然链接到终端。运行java命令后需要运行disown

java -jar yourApp.jar > log.txt &
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 run disown after running the java command.

java -jar yourApp.jar > log.txt &
disown

Now, you can close the terminal.

为你拒绝所有暧昧 2025-01-15 20:37:04

这里需要的关键词是“守护进程”。有没有想过为什么 Linux / Unix 上的系统服务器进程通常以“d”结尾?由于历史原因,“d”代表“守护进程”。

因此,分离并成为真正的服务器进程的过程称为“守护进程”。

它是完全通用的,不仅仅限于 Java 进程。

为了成为真正独立的守护进程,您需要执行几项任务。它们列在维基百科页面上。

您需要担心的两件主要事情是:

  • 与父进程分离 与
  • 创建该进程的 tty 分离

如果您在 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:

  • Detach from parent process
  • Detach from the tty that created the process

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.

夏夜暖风 2025-01-15 20:37:04

尝试,

 java -jar yourApp.jar &

& 将启动新的进程线程,我还没有对此进行测试,但如果仍然不起作用,则将其添加到脚本文件中并使用 & 启动我

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 &

美煞众生 2025-01-15 20:37:04

你有没有尝试把&在命令行末尾?
例如:

java -jar mySocketApp.jar &

您还可以使用bgfg命令将进程发送到后台和前台。您可以通过CTRL+Z暂停正在运行的进程。

查看这篇文章:http://lowfatlinux.com/linux-processes.html

Did you try putting & at the end of the command line?
For example:

java -jar mySocketApp.jar &

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

何其悲哀 2025-01-15 20:37:04

第 1 步。

创建新屏幕

screen -RD screenname

第 2 步。

进入屏幕终端

按 Enter

第 3 步。

运行您的在新打开的终端中运行命令或脚本(在后台运行)

第 4 步。

要退出屏幕终端

ctrl + A + D

第 5 步。< /strong>

列出

screen -ls

将打印如下内容的

屏幕终端有屏幕上:
994.screenname (12/10/2018 09:24:31 AM)(分离)
/run/screen/S-contact 中的 1 个套接字。

第 6 步.

登录后台进程

screen -rd 994.screenname

Step 1.

To create new screen

screen -RD screenname

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

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.

Step 6.

To login to the background process

screen -rd 994.screenname
Hello爱情风 2025-01-15 20:37:04

对于相当终端且此过程仍在工作的背景。对我来说,在后台运行该进程的简单快速的方法是在命令末尾使用 &!

  • 如果此应用程序是为 X 服务器构建的:(例如:Firefox、Zathura、 Gimp...)
$ java -jar yourApp.jar &!
  • 如果这个应用程序是 cli (在终端上工作)
# st is my terminal like kitty alacritty
$ st -e bash -c "lookatme --style one-dark --one $1" &!

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:

  • if this app is built for X server: (eg: Firefox,Zathura,Gimp...)
$ java -jar yourApp.jar &!
  • if this app is cli (work on the terminal)
# st is my terminal like kitty alacritty
$ st -e bash -c "lookatme --style one-dark --one $1" &!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文