在Ubuntu中运行Java无限循环

时间:2021-02-05 13:54:11

Hello I am making a Java program in an Ubuntu Web Server but it is always supposed to be running in an infinite loop, or at least until I stop it. When I run it in the Ubuntu console it won't allow me to keep using the console. To work around this I have been using the "screen" command and detaching the screen. I was wondering if there is a better way of doing this without working with the screen command?

您好我在Ubuntu Web服务器中创建Java程序,但它总是应该在无限循环中运行,或者至少在我停止之前。当我在Ubuntu控制台中运行它时,它将不允许我继续使用控制台。为了解决这个问题,我一直在使用“屏幕”命令并分离屏幕。我想知道如果没有使用屏幕命令有更好的方法吗?

1 个解决方案

#1


0  

Run a program immune to hangups

If you're happy with starting the Java program up manually from the command line, but just want to not have screen running, you can use the "nohup" command to start the Java process in such as way that the Java program will continue running even if you close the console window or log out.

如果您对从命令行手动启动Java程序感到满意,但只是想让屏幕不运行,则可以使用“nohup”命令启动Java进程,以便Java程序继续运行即使您关闭控制台窗口或注销。

$ nohup java ...
nohup: ignoring input and appending output to `nohup.out'
$

Run a program in the background

If you don't mind the Java program stopping if you close the console window or log out, you can skip using "nohup" and only append a "&" to the end of the command to tell your shell to run the application in the background. You may also want to add " > program.log 2>&1" to the command to avoid having any output from the program show up in the console window while you're using it for other purposes.

如果你不介意Java程序停止,如果你关闭控制台窗口或注销,你可以跳过使用“nohup”并只在命令的末尾附加一个“&”来告诉你的shell运行应用程序背景。您可能还需要在命令中添加“> program.log 2>&1”,以避免在您将其用于其他目的时在控制台窗口中显示程序的任何输出。

$ java ... > program.log 2>&1 &
[2] 3128
$ jobs
[2]+ Running            java ... > program.log 2>&1 &
$

Run a program as a daemon

If you want the Java program to automatically start up every time you reboot the machine, you should look into creating a SYSV init-script, or as you're running on Ubuntu, an Upstart Job definiton.

如果您希望每次重新启动计算机时Java程序都自动启动,您应该考虑创建一个SYSV init脚本,或者在Ubuntu上运行,这是一个Upstart Job定义。

#1


0  

Run a program immune to hangups

If you're happy with starting the Java program up manually from the command line, but just want to not have screen running, you can use the "nohup" command to start the Java process in such as way that the Java program will continue running even if you close the console window or log out.

如果您对从命令行手动启动Java程序感到满意,但只是想让屏幕不运行,则可以使用“nohup”命令启动Java进程,以便Java程序继续运行即使您关闭控制台窗口或注销。

$ nohup java ...
nohup: ignoring input and appending output to `nohup.out'
$

Run a program in the background

If you don't mind the Java program stopping if you close the console window or log out, you can skip using "nohup" and only append a "&" to the end of the command to tell your shell to run the application in the background. You may also want to add " > program.log 2>&1" to the command to avoid having any output from the program show up in the console window while you're using it for other purposes.

如果你不介意Java程序停止,如果你关闭控制台窗口或注销,你可以跳过使用“nohup”并只在命令的末尾附加一个“&”来告诉你的shell运行应用程序背景。您可能还需要在命令中添加“> program.log 2>&1”,以避免在您将其用于其他目的时在控制台窗口中显示程序的任何输出。

$ java ... > program.log 2>&1 &
[2] 3128
$ jobs
[2]+ Running            java ... > program.log 2>&1 &
$

Run a program as a daemon

If you want the Java program to automatically start up every time you reboot the machine, you should look into creating a SYSV init-script, or as you're running on Ubuntu, an Upstart Job definiton.

如果您希望每次重新启动计算机时Java程序都自动启动,您应该考虑创建一个SYSV init脚本,或者在Ubuntu上运行,这是一个Upstart Job定义。